#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int32_t main() {
int n, K, neg = 0;
cin >> n >> K;
vector<int> v(n+1);
vector<ll> pref(n+1);
for(int i=1; i<=n; i++) {
cin >> v[i];
neg += (v[i] < 0);
pref[i] = pref[i-1] + v[i];
}
if(neg == 0) {
cout << pref[n] << '\n';
return 0;
}
if(K == 1) {
ll ans = 0, sum = 0, mn = 0;
for(int i=1; i<=n; i++) {
sum += v[i];
ans = max(ans, sum - mn);
mn = min(mn, sum);
}
cout << ans << '\n';
return 0;
}
ll dp[n+1][K+1], mx[K+1];
memset(dp, 0, sizeof(dp));
memset(mx, 0, sizeof(mx));
for(int i=1; i<=n; i++) {
for(int j=0; j<=K; j++) {
dp[i][j] = dp[i-1][j];
if(j) dp[i][j] = max(dp[i][j], pref[i] + mx[j-1]);
}
for(int j=0; j<=K; j++) mx[j] = max(mx[j], dp[i][j] - pref[i]);
}
cout << dp[n][K] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
4956 KB |
Output is correct |
2 |
Correct |
86 ms |
5200 KB |
Output is correct |
3 |
Correct |
81 ms |
5116 KB |
Output is correct |
4 |
Correct |
80 ms |
5084 KB |
Output is correct |
5 |
Correct |
81 ms |
4952 KB |
Output is correct |
6 |
Correct |
78 ms |
4956 KB |
Output is correct |
7 |
Correct |
88 ms |
5204 KB |
Output is correct |
8 |
Correct |
80 ms |
4952 KB |
Output is correct |
9 |
Correct |
79 ms |
4988 KB |
Output is correct |
10 |
Correct |
93 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
4952 KB |
Output is correct |
2 |
Correct |
45 ms |
4956 KB |
Output is correct |
3 |
Correct |
43 ms |
4952 KB |
Output is correct |
4 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
5072 KB |
Output is correct |
2 |
Correct |
85 ms |
4952 KB |
Output is correct |
3 |
Correct |
99 ms |
5048 KB |
Output is correct |
4 |
Correct |
85 ms |
4956 KB |
Output is correct |
5 |
Correct |
89 ms |
5056 KB |
Output is correct |
6 |
Correct |
86 ms |
4956 KB |
Output is correct |
7 |
Correct |
87 ms |
5208 KB |
Output is correct |
8 |
Correct |
86 ms |
5052 KB |
Output is correct |
9 |
Correct |
88 ms |
5108 KB |
Output is correct |
10 |
Correct |
87 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
3420 KB |
Output is correct |
22 |
Correct |
21 ms |
26024 KB |
Output is correct |
23 |
Correct |
5 ms |
6232 KB |
Output is correct |
24 |
Correct |
3 ms |
3676 KB |
Output is correct |
25 |
Correct |
6 ms |
4956 KB |
Output is correct |
26 |
Correct |
3 ms |
3416 KB |
Output is correct |
27 |
Correct |
6 ms |
4956 KB |
Output is correct |
28 |
Correct |
1 ms |
856 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
4956 KB |
Output is correct |
2 |
Correct |
86 ms |
5200 KB |
Output is correct |
3 |
Correct |
81 ms |
5116 KB |
Output is correct |
4 |
Correct |
80 ms |
5084 KB |
Output is correct |
5 |
Correct |
81 ms |
4952 KB |
Output is correct |
6 |
Correct |
78 ms |
4956 KB |
Output is correct |
7 |
Correct |
88 ms |
5204 KB |
Output is correct |
8 |
Correct |
80 ms |
4952 KB |
Output is correct |
9 |
Correct |
79 ms |
4988 KB |
Output is correct |
10 |
Correct |
93 ms |
4956 KB |
Output is correct |
11 |
Correct |
45 ms |
4952 KB |
Output is correct |
12 |
Correct |
45 ms |
4956 KB |
Output is correct |
13 |
Correct |
43 ms |
4952 KB |
Output is correct |
14 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |