# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1025647 | Zicrus | Feast (NOI19_feast) | C++17 | 0 ms | 0 KiB |
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;
typedef long long ll;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (n <= 2000 && k <= 2000) {
vector<vector<ll>> dp(n, vector<ll>(k+1));
vector<vector<ll>> dpSeg(n, vector<ll>(k+1));
for (int j = 1; j <= k; j++) {
dpSeg[0][j] = max(0ll, a[0]);
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= k; j++) {
dp[i][j] = max(dp[i-1][j], dpSeg[i-1][j]);
dpSeg[i][j] = max(dp[i-1][j-1], dpSeg[i-1][j]) + a[i];
}
}
cout << max(dp.back().back(), dpSeg.back().back());
return 0;
}
ll sum = 0;
ll pre = 0, post = 0;
bool neg = false;
for (auto &e : a) {
if (neg) post += e;
if (e < 0) { neg = true; pre = sum; }
sum += e;
}
if (!neg) {
cout << sum;
return;
}
if (k == 1) {
cout << max({pre, post, sum});
}
else {
cout << (pre + post);
}
}