简单背包 容量为所有硬币和的一半
#include#include #include #include using namespace std;int a[110];int f[51000];int main(){ int t; scanf("%d",&t); while(t--) { memset(f, 0, sizeof(f)); int n; scanf("%d",&n); int sum = 0; for(int i = 0; i < n; i++) { scanf("%d",&a[i]); sum += a[i]; } int V = (sum+1)/2; for(int i = 0; i < n; i++) { for(int j = sum; j >= a[i]; j--) { f[j] = max(f[j], f[j-a[i]]+a[i]); } } printf("%d\n",abs(2*f[V]-sum)); } return 0;}