E - The Unbearable Lightness of Weights
思路:
分组背包dp
每组最多只能选一个
一些优化可以快很多
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = , M = 1e4 + ;
int cnt[N];
int dp[N][M];
int main() {
int n, a;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a), cnt[a]++; dp[][] = ;
int dif = , tot = , sum = ;
for (int i = ; i < N; i++) {
if(cnt[i]) {
tot += cnt[i];
sum += cnt[i]*i;
for (int j = sum; j >= i; j--) {
for (int k = ; k <= cnt[i]; k++) {
if(j < k*i) break;
for (int l = k; l <= tot; l++) {
dp[l][j] = min(dp[l][j]+dp[l-k][j-k*i], );
}
}
}
dif++;
}
} if(dif <= ) return *printf("%d\n", n);
int ans = ;
for (int i = ; i < N; i++) {
if(cnt[i]) {
for (int j = ; j <= cnt[i]; j++) if(dp[j][j*i] == ) ans = max(ans, j);
}
}
printf("%d\n", ans);
return ;
}