AcWing 1024. 装箱问题
原题链接
简单
作者:
不知名的fE
,
2024-12-05 18:42:06
,
所有人可见
,
阅读 2
import java.util.*;
public class Main {
static final int N = 20010, M = 35;
static int[][] f = new int[M][N];
static int[] v = new int[M];
static int n, V;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
V = Integer.parseInt(sc.nextLine());
n = Integer.parseInt(sc.nextLine());
for (int i = 1; i <= n; i++)
v[i] = Integer.parseInt(sc.nextLine());
for (int i = 1; i <= n; i++)
for (int j = 1; j <= V; j++) {
f[i][j] = f[i - 1][j];
if (v[i] <= j) f[i][j] = Math.max(f[i][j], f[i - 1][j - v[i]] + v[i]);
}
System.out.println(V - f[n][V]);
}
}