This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #cheat_when_we_are_young
// #cheatkhitacontre #khionhatoicheat
// #thaycuckythatvong
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
using namespace std;
const int NM = 2005, MN = 3e5 + 5;
int n, k, a[MN];
void sub1() {
for (int i = 1; i <= n; ++i) a[i] += a[i-1];
cout << a[n];
}
void sub2() {
int ans = 0;
for (int i = 1; i <= n; ++i) if (a[i] >= 0) ans += a[i];
cout << ans;
}
void sub3() {
int minprefix = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
a[i] += a[i-1];
minprefix = min(minprefix, a[i]);
ans = max(ans, a[i] - minprefix);
}
cout << ans;
}
vector<int> dp_first(NM, 0), dp_second(NM, 0);
void rec(int l, int r, int u, int v) {
if (l > r) return;
int mid = (l+r)>>1, ans = INT_MAX, j, tmp;
for (int i = u; i <= min(v, mid); ++i) {
tmp = dp_first[i-1] + a[i] - a[mid-1];
if (ans > tmp) {
ans = tmp;
j = i;
}
}
dp_second[mid] = ans;
rec(l, mid-1, u, j);
rec(mid+1, r, j, v);
}
void DP() {
for (int i = 1; i <= n; ++i) dp_first[i] = a[i];
for (int i = 1; i <= n; ++i) a[i] += a[i-1];
for (int i = 1; i < k; ++i) {
rec(1, n, 1, n);
dp_first = dp_second;
}
cout << dp_first[n];
}
signed main() {
IOS;
cin >> n >> k;
int check = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] < 0) ++check;
}
if (check == 0) {
sub1();
return 0;
}
if (k == 1) {
sub3();
return 0;
}
if (check == 1) {
sub2();
return 0;
}
DP();
}
Compilation message (stderr)
feast.cpp: In function 'void rec(long long int, long long int, long long int, long long int)':
feast.cpp:41:8: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | rec(l, mid-1, u, j);
| ~~~^~~~~~~~~~~~~~~~
# | 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... |