绝世傻DP
#include <cstdio> #include <iostream> #define N 51 int n, s, mx; bool f[N][1001]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } int main() { int i, j, k, x; n = read(); s = read(); mx = read(); f[0][s] = 1; for(i = 1; i <= n; i++) { x = read(); for(j = 0; j <= mx; j++) { if(j + x <= mx && f[i - 1][j]) f[i][j + x] = 1; if(j - x >= 0 && f[i - 1][j]) f[i][j - x] = 1; } } for(i = mx; i >= 0; i--) if(f[n][i]) { printf("%d\n", i); return 0; } puts("-1"); return 0; }
时间: 2024-10-25 13:28:12