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 maxn = 50, maxm = 1e3 + 10, inf = 1e6;
int dp[maxn][maxm][maxm], v[maxn], n, pref[maxn];
int solve(int i, int val1, int val2)
{
if(dp[i][val1][val2] != -1) return dp[i][val1][val2];
if(i == n)
{
if(val1 == val2)
{
return (pref[n - 1] - val1);
}
else
return inf;
}
int caso1 = solve(i + 1, val1, val2);
int caso2 = solve(i + 1, val1 + v[i], val2);
int caso3 = solve(i + 1, val1, val2 + v[i]);
return dp[i][val1][val2] = min({caso1, caso2, caso3});
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> v[i];
pref[i] = v[i];
if(i) pref[i] += pref[i - 1];
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0, 0) << "\n";
}
# | 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... |