# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1136830 | b1t_exe | Knapsack (NOI18_knapsack) | Java | 0 ms | 0 KiB |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 1; // Number of test cases
while (t-- > 0) {
int x = sc.nextInt(); // Maximum weight capacity
int n = sc.nextInt(); // Number of items
int[][] arr = new int[n][3]; // Item details: value, weight, max quantity
for (int i = 0; i < n; i++) {
arr[i][0] = sc.nextInt(); // Value of item
arr[i][1] = sc.nextInt(); // Weight of item
arr[i][2] = sc.nextInt(); // Maximum quantity of item
}
int[] dp = new int[x + 1]; // DP array to store max profit for each capacity
// Process each item
for (int i = 0; i < n; i++) {
int value = arr[i][0];
int weight = arr[i][1];
int maxQuantity = arr[i][2];
// Process for bounded quantity
for (int capacity = x; capacity >= weight; capacity--) {
for (int quantity = 1; quantity <= maxQuantity && quantity * weight <= capacity; quantity++) {
dp[capacity] = Math.max(dp[capacity], quantity * value + dp[capacity - quantity * weight]);
}
}
}
System.out.println(dp[x]); // Maximum profit for capacity x
}
sc.close();
}
}