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], dp[NM][NM];
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;
}
void nnk() {
for (int i = 1; i <= n; ++i) a[i] += a[i-1];
for (int i = 1; i <= n; ++i) for (int j = 1; j <= k; ++j) {
dp[i][j] = max(dp[i][j], dp[i-1][j]);
for (int p = 1; p <= i; ++p) dp[i][j] = max(dp[i][j], dp[p-1][j-1] + a[i] - a[p-1]);
}
cout << dp[n][k];
}
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;
}
nnk();
}
# | 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... |