题解 | #装箱问题#
装箱问题
http://www.nowcoder.com/practice/c990bd0bf8e04bfbb19c0964401c8f77
01背包问题,第一问
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int v = sc.nextInt();
int n = sc.nextInt();
int[] weights = new int[n];
for(int i = 0; i < n; i++) {
weights[i] = sc.nextInt();
}
int[] dp = new int[v + 1];
for(int i = 0; i < n; i++) {
for(int j = v; j >= weights[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - weights[i]] + weights[i]);
}
}
System.out.println(v - dp[v]);
}
}