解题思路
相关代码
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int v = scanner.nextInt();
int n = scanner.nextInt();
int a[] = new int[n+1];
for(int i=1;i<=n;i++) a[i]=scanner.nextInt();
int f[][] = new int[40][20010];
for(int i=1;i<=n;i++){
for(int j=0;j<=v;j++){
//不选
f[i][j]=f[i-1][j];
if(j>=a[i])
f[i][j]=Math.max(f[i][j],f[i-1][j-a[i]]+a[i]);
}
}
System.out.println(v-f[n][v]);
}
}