This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
int INF = (int)2e9;
using namespace std;
template <class T>
istream& operator >>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
int n;
int dp[500][(int)1e5 + 1];
vi num;
int kek(int pos, int sum){
if (pos == n && sum == 0)
return 0;
if (pos == n)
return -INF;
if (dp[pos][sum] != -1)
return dp[pos][sum];
int a = kek(pos + 1, sum);
int b = num[pos] + kek(pos + 1, sum + num[pos]);
int c = kek(pos + 1, sum - num[pos]);
dp[pos][sum] = max({a, b, c});
return dp[pos][sum];
}
void solve() {
cin >> n;
num.assign(n, 0); cin >> num;
for (int i = 0; i <= n; ++i){
for (int j = 0; j <= (int)5e4; ++j){
dp[i][j] = -1;
}
}
int sum = 0;
for (int i = 0; i < n; ++i){
sum += num[i];
}
cout << sum - kek(0, 0);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#endif
solve();
return 0;
}
# | 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... |