이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
import java.io.*;
public class knapsack {
public static void main(String[] args) throws IOException{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(r.readLine());
int s = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken());
long[] dp = new long[s + 1];
Arrays.fill(dp, -1); dp[0] = 0;
HashMap<Integer, ArrayList<int[]>> weights = new HashMap<>();
for (int i = 0; i < n; i++) {
st = new StringTokenizer(r.readLine());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
ArrayList<int[]> temp = weights.getOrDefault(w, new ArrayList<>());
temp.add(new int[]{v, k});
weights.put(w, temp);
}
for(int e : weights.keySet()){
// System.out.println(e);
// for(int[] e1 : weights.get(e)){
// System.out.println(Arrays.toString(e1));
// }
for (int i = s; i >= 0; i--) {
if(dp[i] != -1){
ArrayList<int[]> cur = weights.get(e);
Collections.sort(cur, (a, b) -> -Integer.compare(a[0], b[0]));
int p = 0;
int z = i;
while(z <= s && p < cur.size()){
for (int j = 1; j <= cur.get(p)[1] && z + j * e <= s; j++) {
dp[z + j * e] = Math.max(dp[z + j * e], dp[z + (j - 1) * e] + cur.get(p)[0]);
}
z += cur.get(p)[1] * e;
p ++;
}
// System.out.println(i + " " + Arrays.toString(dp) + " " + p + " " + cur.size());
}
}
}
long max = -1;
for(long e : dp){
max = Math.max(max, e);
}
System.out.println(max);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |