This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int S = 1e5 + 10;
int n, a[N];
vector<int> dp[S];
bool mark[S], poss[S];
int main(){
cin >> n;
int total = 0;
for (int i = 0; i < n; i ++)
cin >> a[i], total += a[i];
dp[0] = {-1};
for (int i = 0; i < n; i ++){
for (int sm = total; sm >= a[i]; sm--){
if (dp[sm - a[i]].size() == 0) continue;
dp[sm] = dp[sm - a[i]];
dp[sm].push_back(i);
}
}
int ite = 0;
poss[0] = 1;
int mx = -1;
for (int X = total / 2; X >= 0; X --){
if (ite >= 1e8) break;
// cout << "Starting to check " << X << endl;
for (int i : dp[X]){
// cout << "marking index = " << i << endl;
ite++;
if (~i) mark[i] = 1;
}
for (int i = 0; i < n; i ++){
if (mark[i]){
mark[i] = 0;
ite++;
continue;
}
for (int sm = X; sm >= a[i]; sm --){
ite++;
poss[sm] |= poss[sm - a[i]];
}
}
if (poss[X]){
mx = X;
break;
}
for (int i = 1; i <= X; i ++){
ite++;
poss[i] = 0;
}
}
if (~mx){
cout << total - mx << endl;
return 0;
}
for (int x = 0; 2 * x < S; x ++)
if (dp[x].size() and dp[2 * x].size())
mx = max(mx, x);
cout << mx + (total - 2 * mx) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |