#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k, arr[300300], l, r, vis[300300], mx = -1e18, sum, now, ans, qs[300300], ck, idx;
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for(int i = 1; i<=n; ++i){
cin >> arr[i];
if(arr[i] < 0){
ck++;
idx = i;
}
qs[i] = qs[i - 1] + arr[i];
}
if(!ck){
cout << qs[n];
return 0;
}
if(ck == 1){
if(k == 1) cout << max({qs[idx - 1], qs[n], qs[n] - qs[idx]});
else cout << qs[n] - arr[idx];
return 0;
}
if(k == 1){
for(int i = 1; i<=n; ++i){
sum+=arr[i];
if(sum < 0) sum = 0;
ans = max(ans, sum);
}
cout << ans;
return 0;
}
int 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], qs[i] + mx[j-1]);
}
for(int j=0; j<=k; j++) mx[j] = max(mx[j], dp[i][j] - qs[i]);
}
cout << dp[n][k];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
10212 KB |
Output is correct |
2 |
Correct |
35 ms |
10228 KB |
Output is correct |
3 |
Correct |
24 ms |
10348 KB |
Output is correct |
4 |
Correct |
25 ms |
10120 KB |
Output is correct |
5 |
Correct |
36 ms |
10068 KB |
Output is correct |
6 |
Correct |
23 ms |
10112 KB |
Output is correct |
7 |
Correct |
23 ms |
10064 KB |
Output is correct |
8 |
Correct |
23 ms |
10116 KB |
Output is correct |
9 |
Correct |
25 ms |
10232 KB |
Output is correct |
10 |
Correct |
35 ms |
10156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
8568 KB |
Output is correct |
2 |
Correct |
15 ms |
8540 KB |
Output is correct |
3 |
Correct |
16 ms |
8456 KB |
Output is correct |
4 |
Correct |
19 ms |
8360 KB |
Output is correct |
5 |
Correct |
30 ms |
9980 KB |
Output is correct |
6 |
Correct |
16 ms |
8540 KB |
Output is correct |
7 |
Correct |
23 ms |
8540 KB |
Output is correct |
8 |
Correct |
25 ms |
10324 KB |
Output is correct |
9 |
Correct |
23 ms |
10104 KB |
Output is correct |
10 |
Correct |
19 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
10320 KB |
Output is correct |
2 |
Correct |
39 ms |
10228 KB |
Output is correct |
3 |
Correct |
27 ms |
10360 KB |
Output is correct |
4 |
Correct |
26 ms |
10332 KB |
Output is correct |
5 |
Correct |
40 ms |
10244 KB |
Output is correct |
6 |
Correct |
26 ms |
10320 KB |
Output is correct |
7 |
Correct |
26 ms |
10324 KB |
Output is correct |
8 |
Correct |
40 ms |
10228 KB |
Output is correct |
9 |
Correct |
28 ms |
10488 KB |
Output is correct |
10 |
Correct |
40 ms |
10432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2400 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2400 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2400 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2564 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2516 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2400 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2516 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2404 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
5468 KB |
Output is correct |
22 |
Correct |
19 ms |
28076 KB |
Output is correct |
23 |
Correct |
6 ms |
8288 KB |
Output is correct |
24 |
Correct |
3 ms |
5728 KB |
Output is correct |
25 |
Correct |
4 ms |
7008 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
6880 KB |
Output is correct |
28 |
Correct |
1 ms |
2904 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
10212 KB |
Output is correct |
2 |
Correct |
35 ms |
10228 KB |
Output is correct |
3 |
Correct |
24 ms |
10348 KB |
Output is correct |
4 |
Correct |
25 ms |
10120 KB |
Output is correct |
5 |
Correct |
36 ms |
10068 KB |
Output is correct |
6 |
Correct |
23 ms |
10112 KB |
Output is correct |
7 |
Correct |
23 ms |
10064 KB |
Output is correct |
8 |
Correct |
23 ms |
10116 KB |
Output is correct |
9 |
Correct |
25 ms |
10232 KB |
Output is correct |
10 |
Correct |
35 ms |
10156 KB |
Output is correct |
11 |
Correct |
15 ms |
8568 KB |
Output is correct |
12 |
Correct |
15 ms |
8540 KB |
Output is correct |
13 |
Correct |
16 ms |
8456 KB |
Output is correct |
14 |
Correct |
19 ms |
8360 KB |
Output is correct |
15 |
Correct |
30 ms |
9980 KB |
Output is correct |
16 |
Correct |
16 ms |
8540 KB |
Output is correct |
17 |
Correct |
23 ms |
8540 KB |
Output is correct |
18 |
Correct |
25 ms |
10324 KB |
Output is correct |
19 |
Correct |
23 ms |
10104 KB |
Output is correct |
20 |
Correct |
19 ms |
8532 KB |
Output is correct |
21 |
Correct |
39 ms |
10320 KB |
Output is correct |
22 |
Correct |
39 ms |
10228 KB |
Output is correct |
23 |
Correct |
27 ms |
10360 KB |
Output is correct |
24 |
Correct |
26 ms |
10332 KB |
Output is correct |
25 |
Correct |
40 ms |
10244 KB |
Output is correct |
26 |
Correct |
26 ms |
10320 KB |
Output is correct |
27 |
Correct |
26 ms |
10324 KB |
Output is correct |
28 |
Correct |
40 ms |
10228 KB |
Output is correct |
29 |
Correct |
28 ms |
10488 KB |
Output is correct |
30 |
Correct |
40 ms |
10432 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2400 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2564 KB |
Output is correct |
36 |
Correct |
1 ms |
2516 KB |
Output is correct |
37 |
Correct |
1 ms |
2392 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2648 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2516 KB |
Output is correct |
42 |
Correct |
1 ms |
2652 KB |
Output is correct |
43 |
Correct |
1 ms |
2400 KB |
Output is correct |
44 |
Correct |
1 ms |
2400 KB |
Output is correct |
45 |
Correct |
1 ms |
2516 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2404 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2392 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
3 ms |
5468 KB |
Output is correct |
52 |
Correct |
19 ms |
28076 KB |
Output is correct |
53 |
Correct |
6 ms |
8288 KB |
Output is correct |
54 |
Correct |
3 ms |
5728 KB |
Output is correct |
55 |
Correct |
4 ms |
7008 KB |
Output is correct |
56 |
Correct |
3 ms |
5212 KB |
Output is correct |
57 |
Correct |
4 ms |
6880 KB |
Output is correct |
58 |
Correct |
1 ms |
2904 KB |
Output is correct |
59 |
Correct |
1 ms |
2908 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Runtime error |
188 ms |
262144 KB |
Execution killed with signal 9 |
62 |
Halted |
0 ms |
0 KB |
- |