题目描述
dp(不存负数,所以就多一倍)
样例
import java.util.*;
public class Main{
static int N = 110, M = 200010, B = M / 2;
static int n, m;
static int w[] = new int[N];
static boolean f[][] = new boolean[N][M];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i ++ ){
w[i] = sc.nextInt();
m += w[i];
}
f[0][B] = true;
for(int i = 1; i <= n;i ++){
for(int j = -m; j <= m; j ++){
//不选
f[i][j + B] = f[i - 1][j + B];
//正选
if (j - w[i] >= -m) f[i][j + B] |= f[i - 1][j - w[i] + B];
//负选
if (j + w[i] <= m) f[i][j + B] |= f[i - 1][j + w[i] + B];
}
}
int res = 0;
for (int j = 1; j <= m; j ++ ){
if (f[n][j + B]){
res ++ ;
}
}
System.out.println(res);
}
}