Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e2 + 1;
int n, a[N], fr[N];
short dp[N][N][N*N];
short rec(int idx, int rem, int sum) {
if(rem < 0 || sum < 0)
return 0;
if(idx == N)
return rem == 0 && sum == 0;
short &ret = dp[idx][rem][sum];
if(ret != -1)
return ret;
ret = min(2, int(rec(idx + 1, rem, sum)));
for(int i = 1; i <= fr[idx]; ++i)
ret = min(2, ret + rec(idx + 1, rem - i, sum - idx * i));
return ret;
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", a + i), ++fr[a[i]];
int cnt = 0;
for(int i = 0; i < N; ++i)
cnt += fr[i] != 0;
if(cnt <= 2) {
printf("%d\n", n);
return 0;
}
memset(dp, -1, sizeof dp);
int res = 0;
for(int i = 1; i < N; ++i)
for(int j = 1; j <= fr[i]; ++j)
if(rec(1, j, i * j) == 1)
res = max(res, j);
printf("%d\n", res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
4
1 4 2 2
Output
2
Demonstration
Codeforces Solution-E. The Unbearable Lightness of Weights-Solution in C, C++, Java, Python,The Unbearable Lightness of Weights,Codeforces Solution