You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 30;
const int M = 1e4 + 10;
/*
2
3 5
4
3*5-3-5=7
1 2 4 7
*/
int f[N][M];
int w[N];
int res;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> w[i];
sort(w + 1, w + 1 + n);
int m1 = w[1], m2 = w[2];
int M = m1 * m2 - m1 - m2; // a*b-a-b =7
int g = w[1];
for (int i = 2; i <= n; i++) g = gcd(g, w[i]);
if (g > 1) {
cout << -1 << endl;
return 0;
}
// 完全背包二维写法
for (int i = 1; i <= n; i++)
for (int j = 1; j <= M; j++)
if (j >= w[i])
f[i][j] = max(f[i - 1][j], f[i][j - w[i]] + w[i]);
else
f[i][j] = f[i - 1][j];
for (int i = 1; i <= M; i++)
if (f[n][i] < i) res++;
printf("%d\n", res);
return 0;
}