forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.java
73 lines (65 loc) · 2.19 KB
/
D.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.PrintStream;
import java.util.*;
/**
* APAC 2017 Round A Problem D: Clash Royale
* Check README.md for explanation.
*/
public class Main {
class Card {
int k, l;
long[] a, c;
public Card(int _k, int _l) {
k=_k;l=_l;
a=new long[k+1];
c=new long[k+1];
}
}
private String solve(Scanner scanner) {
long m=scanner.nextLong();
int n=scanner.nextInt();
Card[] cards=new Card[n];
long[] maxAtk=new long[n];
for (int i=0;i<n;i++) {
cards[i]=new Card(scanner.nextInt(), scanner.nextInt());
int k=cards[i].k;
for (int j=1;j<=k;j++) {
cards[i].a[j]=scanner.nextLong();
}
for (int j=1;j<k;j++) {
cards[i].c[j]=scanner.nextLong();
}
maxAtk[i]=(i==0?0:maxAtk[i-1])+cards[i].a[cards[i].k];
}
dfs(m, n-1, cards, Math.min(n, 8), 0, maxAtk);
return String.valueOf(result);
}
private long result=0;
private void dfs(long m, int n, Card[] cards, int remain, long current, long[] maxAtk) {
if (n<0 || remain==0) {
if (current>result) result=current;
return;
}
// prune
if (current+maxAtk[n]<=result) return;
dfs(m, n-1, cards, remain, current, maxAtk);
Card card=cards[n];
dfs(m, n-1, cards, remain-1, current+card.a[card.l], maxAtk);
long need=0;
for (int i=card.l+1;i<=card.k;i++) {
need+=card.c[i-1];
if (m<need) break;
dfs(m-need, n-1, cards, remain-1, current+card.a[i], maxAtk);
}
}
public static void main(String[] args) throws Exception {
//System.setOut(new PrintStream("E:\\desktop\\output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}