- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlchemy.java
More file actions
Latest commit
40 lines (36 loc) · 1.33 KB
/
Copy pathAlchemy.java
File metadata and controls
40 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
importjava.io.*;
importjava.util.*;
publicclassAlchemy {
publicstaticintlol = 0;
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderin = newBufferedReader(newInputStreamReader(System.in));
intn = Integer.parseInt(in.readLine());
StringTokenizerst = newStringTokenizer(in.readLine());
int[] have = newint[n];
for(inti = 0; i < n; i++) have[i] = Integer.parseInt(st.nextToken());
intans = 0;
int[][] recipes = newint[n][];
intk = Integer.parseInt(in.readLine());
while(k-- > 0) {
st = newStringTokenizer(in.readLine());
intgain = Integer.parseInt(st.nextToken())-1;
recipes[gain] = newint[Integer.parseInt(st.nextToken())];
for(inti = 0; i < recipes[gain].length; i++) recipes[gain][i] = Integer.parseInt(st.nextToken())-1;
}
while(canMake(recipes, have, n-1)) ans++;
PrintWriterout = newPrintWriter(newBufferedWriter(newOutputStreamWriter(System.out)));
out.println(ans);
out.println(lol);
out.close();
}
privatestaticbooleancanMake(int[][] recipes, int[] have, intwant) {
lol++;
if(have[want] > 0) {
have[want]--;
returntrue;
}
if(recipes[want] == null) returnfalse;
for(intcomponent: recipes[want]) if(!canMake(recipes, have, component)) returnfalse;
returntrue;
}
}