-
Notifications
You must be signed in to change notification settings - Fork 22
/
backpack.java
65 lines (59 loc) · 2.15 KB
/
backpack.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
import java.io.*;
import java.util.*;
import static java.lang.Integer.parseInt;
public class backpack
{
private static BufferedReader f;
public static void main(String[] args) throws IOException {
f = new BufferedReader(new InputStreamReader(System.in));
int t = parseInt(f.readLine());
int[] dp, temp, val, vol, ind;
int[][] att; boolean[] isatt;
int cap, n, i, a, j, k, m;
StringTokenizer st;
while(t-->0)
{
st = new StringTokenizer(f.readLine());
cap = parseInt(st.nextToken());
n = parseInt(st.nextToken());
dp = new int[cap+1];
vol = new int[n]; val = new int[n];
att = new int[n][3]; ind = new int[n];
isatt = new boolean[n];
for(i = 0; i < n; i++)
{
st = new StringTokenizer(f.readLine());
vol[i] = parseInt(st.nextToken());
val[i] = parseInt(st.nextToken())*vol[i];
a = parseInt(st.nextToken());
if(a == 0) continue; a--;
att[a][ind[a]++] = i;
isatt[i] = true;
}
for(i = 0; i < n; i++)
{
if(isatt[i]) continue;
temp = new int[cap+1];
for(k = cap; k >= vol[i]; k--)
temp[k] = dp[k-vol[i]]+val[i];
for(j = 0; j < ind[i]; j++)
{
m = att[i][j];
for(k = cap; k >= vol[i]+vol[m]; k--)
{
if(temp[k] < dp[k-vol[i]-vol[m]]+val[i]+val[m])
temp[k] = dp[k-vol[i]-vol[m]]+val[i]+val[m];
if(temp[k] < temp[k-vol[m]]+val[m])
temp[k] = temp[k-vol[m]]+val[m];
}
}
for(k = 0; k <= cap; k++)
if(dp[k] < temp[k])
dp[k] = temp[k];
}
System.out.println(dp[cap]);
System.out.flush();
}
System.exit(0);
}
}