Submission #469994

#TimeUsernameProblemLanguageResultExecution timeMemory
469994ImaginaryIQKnapsack (NOI18_knapsack)Java
37 / 100
221 ms14324 KiB
import java.util.*; import java.io.*; public class knapsack { static int s, n; static long [][] arr; static long [][] dp; // dp[i][j][k] = the maximmum amount of value that we can using the first i items, // without exceeding weight j, while picking a max of k times public static void main(String[] args) throws Exception { Scanner io = new Scanner(System.in); s = io.nextInt(); n = io.nextInt(); arr = new long[n+1][3]; // v, w, count dp = new long[n+1][s+1]; for (int i = 1; i <= n; i++) { arr[i][0] = io.nextInt(); // value, weight, and, number of picks arr[i][1] = io.nextInt(); arr[i][2] = io.nextInt(); } dp [0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j] = dp[i-1][j]; // go back the number of times taken for (int k = 1; k <= Math.min(arr[i][2], n); k++) { long prev = j - (arr[i][1] * k); if (prev >= 0) { dp[i][j] = Math.max(dp[i-1][(int)prev] + arr[i][0] * k, dp[i][j]); } } } } long ans = 0; for (int i = 0; i <= s; i++) { ans = Math.max(dp[n][i], ans); } System.out.println(ans); io.close(); } static class pair implements Comparable<pair>{ int p1, p2; public pair(int p1, int p2){ this.p1 = p1; this.p2 = p2; } @Override public int compareTo(pair o) { return Integer.compare(this.p1, o.p1); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...