#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;
return 0;
}
s();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
4492 KB |
Output is correct |
2 |
Correct |
89 ms |
4504 KB |
Output is correct |
3 |
Correct |
90 ms |
4424 KB |
Output is correct |
4 |
Correct |
99 ms |
4480 KB |
Output is correct |
5 |
Correct |
88 ms |
4440 KB |
Output is correct |
6 |
Correct |
87 ms |
4420 KB |
Output is correct |
7 |
Correct |
83 ms |
4528 KB |
Output is correct |
8 |
Correct |
90 ms |
4436 KB |
Output is correct |
9 |
Correct |
90 ms |
4508 KB |
Output is correct |
10 |
Correct |
86 ms |
4408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
4504 KB |
Output is correct |
2 |
Correct |
48 ms |
4460 KB |
Output is correct |
3 |
Correct |
55 ms |
4432 KB |
Output is correct |
4 |
Correct |
47 ms |
4512 KB |
Output is correct |
5 |
Correct |
87 ms |
4428 KB |
Output is correct |
6 |
Correct |
47 ms |
4520 KB |
Output is correct |
7 |
Correct |
49 ms |
4428 KB |
Output is correct |
8 |
Correct |
103 ms |
4412 KB |
Output is correct |
9 |
Correct |
101 ms |
4428 KB |
Output is correct |
10 |
Correct |
51 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
4408 KB |
Output is correct |
2 |
Correct |
90 ms |
4468 KB |
Output is correct |
3 |
Correct |
95 ms |
4480 KB |
Output is correct |
4 |
Correct |
93 ms |
4504 KB |
Output is correct |
5 |
Correct |
100 ms |
4488 KB |
Output is correct |
6 |
Correct |
94 ms |
4496 KB |
Output is correct |
7 |
Correct |
99 ms |
4420 KB |
Output is correct |
8 |
Correct |
92 ms |
4408 KB |
Output is correct |
9 |
Correct |
93 ms |
4456 KB |
Output is correct |
10 |
Correct |
101 ms |
4432 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
852 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 |
0 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 |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
852 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 |
0 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 |
25 ms |
21720 KB |
Output is correct |
22 |
Correct |
205 ms |
179360 KB |
Output is correct |
23 |
Correct |
47 ms |
41588 KB |
Output is correct |
24 |
Correct |
24 ms |
23488 KB |
Output is correct |
25 |
Correct |
33 ms |
32468 KB |
Output is correct |
26 |
Correct |
23 ms |
19480 KB |
Output is correct |
27 |
Correct |
31 ms |
30648 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 |
Correct |
91 ms |
4492 KB |
Output is correct |
2 |
Correct |
89 ms |
4504 KB |
Output is correct |
3 |
Correct |
90 ms |
4424 KB |
Output is correct |
4 |
Correct |
99 ms |
4480 KB |
Output is correct |
5 |
Correct |
88 ms |
4440 KB |
Output is correct |
6 |
Correct |
87 ms |
4420 KB |
Output is correct |
7 |
Correct |
83 ms |
4528 KB |
Output is correct |
8 |
Correct |
90 ms |
4436 KB |
Output is correct |
9 |
Correct |
90 ms |
4508 KB |
Output is correct |
10 |
Correct |
86 ms |
4408 KB |
Output is correct |
11 |
Correct |
51 ms |
4504 KB |
Output is correct |
12 |
Correct |
48 ms |
4460 KB |
Output is correct |
13 |
Correct |
55 ms |
4432 KB |
Output is correct |
14 |
Correct |
47 ms |
4512 KB |
Output is correct |
15 |
Correct |
87 ms |
4428 KB |
Output is correct |
16 |
Correct |
47 ms |
4520 KB |
Output is correct |
17 |
Correct |
49 ms |
4428 KB |
Output is correct |
18 |
Correct |
103 ms |
4412 KB |
Output is correct |
19 |
Correct |
101 ms |
4428 KB |
Output is correct |
20 |
Correct |
51 ms |
4424 KB |
Output is correct |
21 |
Correct |
95 ms |
4408 KB |
Output is correct |
22 |
Correct |
90 ms |
4468 KB |
Output is correct |
23 |
Correct |
95 ms |
4480 KB |
Output is correct |
24 |
Correct |
93 ms |
4504 KB |
Output is correct |
25 |
Correct |
100 ms |
4488 KB |
Output is correct |
26 |
Correct |
94 ms |
4496 KB |
Output is correct |
27 |
Correct |
99 ms |
4420 KB |
Output is correct |
28 |
Correct |
92 ms |
4408 KB |
Output is correct |
29 |
Correct |
93 ms |
4456 KB |
Output is correct |
30 |
Correct |
101 ms |
4432 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
852 KB |
Output is correct |
42 |
Correct |
1 ms |
1236 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
980 KB |
Output is correct |
45 |
Correct |
1 ms |
724 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
1108 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
25 ms |
21720 KB |
Output is correct |
52 |
Correct |
205 ms |
179360 KB |
Output is correct |
53 |
Correct |
47 ms |
41588 KB |
Output is correct |
54 |
Correct |
24 ms |
23488 KB |
Output is correct |
55 |
Correct |
33 ms |
32468 KB |
Output is correct |
56 |
Correct |
23 ms |
19480 KB |
Output is correct |
57 |
Correct |
31 ms |
30648 KB |
Output is correct |
58 |
Correct |
4 ms |
3924 KB |
Output is correct |
59 |
Correct |
4 ms |
3540 KB |
Output is correct |
60 |
Correct |
1 ms |
724 KB |
Output is correct |
61 |
Runtime error |
268 ms |
262144 KB |
Execution killed with signal 9 |
62 |
Halted |
0 ms |
0 KB |
- |