#include<bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, k;
int a[N];
long long dp[2003][2003][2];
void sub3()
{
long long ans = 0, mn = 0, s = 0;
for(int i = 1; i <= n; i++)
{
s += a[i];
mn = min(mn, s);
ans = max(ans, s - mn);
}
cout << ans;
}
void sub6()
{
long long 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];
ans = max({ans, dp[i][j][0], dp[i][j][1]});
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
int cntam = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
cntam += (a[i] < 0);
}
if(k == 1)
{
sub3();
return 0;
}
if(cntam <= 1)
{
long long ans = 0;
for(int i = 1; i <= n; i++) if(a[i] > 0) ans += a[i];
cout << ans;
return 0;
}
sub6();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2652 KB |
Output is correct |
2 |
Correct |
22 ms |
2520 KB |
Output is correct |
3 |
Correct |
22 ms |
2516 KB |
Output is correct |
4 |
Correct |
22 ms |
2652 KB |
Output is correct |
5 |
Correct |
22 ms |
2516 KB |
Output is correct |
6 |
Correct |
22 ms |
2508 KB |
Output is correct |
7 |
Correct |
21 ms |
2512 KB |
Output is correct |
8 |
Correct |
22 ms |
2820 KB |
Output is correct |
9 |
Correct |
21 ms |
2652 KB |
Output is correct |
10 |
Correct |
22 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2652 KB |
Output is correct |
2 |
Correct |
14 ms |
2652 KB |
Output is correct |
3 |
Correct |
15 ms |
2516 KB |
Output is correct |
4 |
Correct |
14 ms |
2652 KB |
Output is correct |
5 |
Correct |
22 ms |
2644 KB |
Output is correct |
6 |
Correct |
14 ms |
2612 KB |
Output is correct |
7 |
Correct |
14 ms |
2516 KB |
Output is correct |
8 |
Correct |
22 ms |
2644 KB |
Output is correct |
9 |
Correct |
22 ms |
2652 KB |
Output is correct |
10 |
Correct |
14 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2652 KB |
Output is correct |
2 |
Correct |
25 ms |
5572 KB |
Output is correct |
3 |
Correct |
25 ms |
5692 KB |
Output is correct |
4 |
Correct |
24 ms |
5436 KB |
Output is correct |
5 |
Correct |
25 ms |
5548 KB |
Output is correct |
6 |
Correct |
25 ms |
5512 KB |
Output is correct |
7 |
Correct |
25 ms |
5716 KB |
Output is correct |
8 |
Correct |
24 ms |
5456 KB |
Output is correct |
9 |
Correct |
26 ms |
5708 KB |
Output is correct |
10 |
Correct |
28 ms |
5696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
21 |
Correct |
7 ms |
64092 KB |
Output is correct |
22 |
Correct |
13 ms |
64140 KB |
Output is correct |
23 |
Correct |
8 ms |
64092 KB |
Output is correct |
24 |
Correct |
7 ms |
64092 KB |
Output is correct |
25 |
Correct |
8 ms |
64092 KB |
Output is correct |
26 |
Correct |
7 ms |
64092 KB |
Output is correct |
27 |
Correct |
8 ms |
64092 KB |
Output is correct |
28 |
Correct |
7 ms |
64092 KB |
Output is correct |
29 |
Correct |
7 ms |
63836 KB |
Output is correct |
30 |
Correct |
7 ms |
64032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2652 KB |
Output is correct |
2 |
Correct |
22 ms |
2520 KB |
Output is correct |
3 |
Correct |
22 ms |
2516 KB |
Output is correct |
4 |
Correct |
22 ms |
2652 KB |
Output is correct |
5 |
Correct |
22 ms |
2516 KB |
Output is correct |
6 |
Correct |
22 ms |
2508 KB |
Output is correct |
7 |
Correct |
21 ms |
2512 KB |
Output is correct |
8 |
Correct |
22 ms |
2820 KB |
Output is correct |
9 |
Correct |
21 ms |
2652 KB |
Output is correct |
10 |
Correct |
22 ms |
2512 KB |
Output is correct |
11 |
Correct |
14 ms |
2652 KB |
Output is correct |
12 |
Correct |
14 ms |
2652 KB |
Output is correct |
13 |
Correct |
15 ms |
2516 KB |
Output is correct |
14 |
Correct |
14 ms |
2652 KB |
Output is correct |
15 |
Correct |
22 ms |
2644 KB |
Output is correct |
16 |
Correct |
14 ms |
2612 KB |
Output is correct |
17 |
Correct |
14 ms |
2516 KB |
Output is correct |
18 |
Correct |
22 ms |
2644 KB |
Output is correct |
19 |
Correct |
22 ms |
2652 KB |
Output is correct |
20 |
Correct |
14 ms |
2652 KB |
Output is correct |
21 |
Correct |
23 ms |
2652 KB |
Output is correct |
22 |
Correct |
25 ms |
5572 KB |
Output is correct |
23 |
Correct |
25 ms |
5692 KB |
Output is correct |
24 |
Correct |
24 ms |
5436 KB |
Output is correct |
25 |
Correct |
25 ms |
5548 KB |
Output is correct |
26 |
Correct |
25 ms |
5512 KB |
Output is correct |
27 |
Correct |
25 ms |
5716 KB |
Output is correct |
28 |
Correct |
24 ms |
5456 KB |
Output is correct |
29 |
Correct |
26 ms |
5708 KB |
Output is correct |
30 |
Correct |
28 ms |
5696 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
0 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
12636 KB |
Output is correct |
42 |
Correct |
1 ms |
10588 KB |
Output is correct |
43 |
Correct |
1 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
12636 KB |
Output is correct |
45 |
Correct |
2 ms |
12636 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
12636 KB |
Output is correct |
48 |
Correct |
1 ms |
12636 KB |
Output is correct |
49 |
Correct |
2 ms |
12636 KB |
Output is correct |
50 |
Correct |
1 ms |
12636 KB |
Output is correct |
51 |
Correct |
7 ms |
64092 KB |
Output is correct |
52 |
Correct |
13 ms |
64140 KB |
Output is correct |
53 |
Correct |
8 ms |
64092 KB |
Output is correct |
54 |
Correct |
7 ms |
64092 KB |
Output is correct |
55 |
Correct |
8 ms |
64092 KB |
Output is correct |
56 |
Correct |
7 ms |
64092 KB |
Output is correct |
57 |
Correct |
8 ms |
64092 KB |
Output is correct |
58 |
Correct |
7 ms |
64092 KB |
Output is correct |
59 |
Correct |
7 ms |
63836 KB |
Output is correct |
60 |
Correct |
7 ms |
64032 KB |
Output is correct |
61 |
Runtime error |
286 ms |
133164 KB |
Execution killed with signal 11 |
62 |
Halted |
0 ms |
0 KB |
- |