#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(x) (x).begin(), (x).end()
int n, k;
vector<ll> a = {};
void s(){
vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(k+1, vector<ll>(2, -1e15)));
ll ans=0;
dp[0][0][1]=0; dp[0][0][0]=0;
for(int i=1; i<=n; i++){
dp[i][0][0]=0;
for(int j=1; j<=k; j++){
dp[i][j][0]=max(dp[i-1][j][1], dp[i-1][j][0]);
dp[i][j][1]=max(dp[i-1][j][1], dp[i-1][j-1][0])+a[i-1];
// cout << i << ' ' << j << ' ' << dp[i][j][0] << ' ' << dp[i][j][1] << endl;
ans=max({ans,dp[i][j][0],dp[i][j][1]});
}
}
cout << ans << endl;
}
void f() {
ll ans = 0, nw = 0;
for (auto& u : a) {
nw = max(0LL, nw + u);
ans = max(ans, nw);
}
cout << ans << endl;
return;
}
int main() {
cin >> n >> k; int cntn=0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
a.pb(x);
if(x<0) cntn++;
}
if (k == 1) {
f();
return 0;
}
if(cntn<=1){
ll cnt = 0;
for (auto& u : a)
if (u > 0) cnt += u;
cout << cnt << endl;
}
s();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
263 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
4412 KB |
Output is correct |
2 |
Correct |
48 ms |
4480 KB |
Output is correct |
3 |
Correct |
47 ms |
4460 KB |
Output is correct |
4 |
Runtime error |
217 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
4532 KB |
Output is correct |
2 |
Correct |
91 ms |
4520 KB |
Output is correct |
3 |
Correct |
98 ms |
4556 KB |
Output is correct |
4 |
Correct |
94 ms |
4464 KB |
Output is correct |
5 |
Correct |
94 ms |
4408 KB |
Output is correct |
6 |
Correct |
96 ms |
4408 KB |
Output is correct |
7 |
Correct |
97 ms |
4408 KB |
Output is correct |
8 |
Correct |
93 ms |
4480 KB |
Output is correct |
9 |
Correct |
94 ms |
4408 KB |
Output is correct |
10 |
Correct |
93 ms |
4416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
26 ms |
21860 KB |
Output is correct |
22 |
Correct |
169 ms |
179252 KB |
Output is correct |
23 |
Correct |
42 ms |
41548 KB |
Output is correct |
24 |
Correct |
25 ms |
23380 KB |
Output is correct |
25 |
Correct |
34 ms |
32456 KB |
Output is correct |
26 |
Correct |
20 ms |
19412 KB |
Output is correct |
27 |
Correct |
31 ms |
30548 KB |
Output is correct |
28 |
Correct |
4 ms |
3924 KB |
Output is correct |
29 |
Correct |
4 ms |
3540 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
263 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |