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 = 510;
const int maxm = 1e5+10;
const int inf = 1e9+10;
int num[maxn], n;
int dp[maxn][maxm];
int solve(int pos, int soma)
{
if (pos == n+1 && !soma) return 0;
if (pos == n+1) return -inf;
if (dp[pos][soma] != -1) return dp[pos][soma];
int caso1 = num[pos]+solve(pos+1, soma+num[pos]);
int caso2 = solve(pos+1, soma-num[pos]);
int caso3 = solve(pos+1, soma);
return dp[pos][soma] = max({caso1, caso2, caso3});
}
int main(void)
{
int soma = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> num[i];
soma += num[i];
}
memset(dp, -1, sizeof dp);
cout << soma-solve(1, 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... |