//In The Name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
const int N = 3e5 + 10;
const int M = 2010;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 10;
ll a[N], dp[2][M][M];
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, k, mn = inf, cn = 0, sum = 0;
cin >> n >> k;
for (int i = 1; i <= n; i ++){
cin >> a[i];
mn = min(mn, a[i]);
cn += a[i] < 0;
sum += a[i];
}
if (0 <= mn)
return cout << sum, 0;
if (cn == 1){
ll pre = 0, suf = 0;
for (int i = 1; i <= n && 0 <= a[i]; i ++)
pre += a[i];
for (int i = n; i >= 1 && 0 <= a[i]; i --)
suf += a[i];
if (2 <= k)
return cout << sum - mn, 0;
return cout << max({sum, pre, suf}), 0;
}
if (k == 1){
ll ans = 0, cur = 0;
for (int i = 1; i <= n; i ++){
cur = max(0LL, cur + a[i]);
ans = max(ans, cur);
}
return cout << ans, 0;
}
if (n <= 2000 && k <= 2000){
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= k; j ++){
dp[0][i][j] = a[i] + max(dp[0][i - 1][j], dp[1][i - 1][j - 1]);
dp[1][i][j] = max(dp[1][i - 1][j], dp[0][i][j]);
}
}
return cout << dp[1][n][k], 0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
5484 KB |
Output is correct |
2 |
Correct |
35 ms |
5484 KB |
Output is correct |
3 |
Correct |
37 ms |
5612 KB |
Output is correct |
4 |
Correct |
35 ms |
5612 KB |
Output is correct |
5 |
Correct |
38 ms |
5740 KB |
Output is correct |
6 |
Correct |
35 ms |
5484 KB |
Output is correct |
7 |
Correct |
33 ms |
5484 KB |
Output is correct |
8 |
Correct |
35 ms |
5484 KB |
Output is correct |
9 |
Correct |
34 ms |
5484 KB |
Output is correct |
10 |
Correct |
38 ms |
5612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
3820 KB |
Output is correct |
2 |
Correct |
24 ms |
3820 KB |
Output is correct |
3 |
Correct |
23 ms |
3692 KB |
Output is correct |
4 |
Correct |
23 ms |
3820 KB |
Output is correct |
5 |
Correct |
34 ms |
5484 KB |
Output is correct |
6 |
Correct |
23 ms |
3820 KB |
Output is correct |
7 |
Correct |
24 ms |
3948 KB |
Output is correct |
8 |
Correct |
37 ms |
5612 KB |
Output is correct |
9 |
Correct |
35 ms |
5356 KB |
Output is correct |
10 |
Correct |
23 ms |
3820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
5740 KB |
Output is correct |
2 |
Correct |
37 ms |
5612 KB |
Output is correct |
3 |
Correct |
45 ms |
5740 KB |
Output is correct |
4 |
Correct |
41 ms |
5612 KB |
Output is correct |
5 |
Correct |
39 ms |
5740 KB |
Output is correct |
6 |
Correct |
39 ms |
5740 KB |
Output is correct |
7 |
Correct |
40 ms |
5740 KB |
Output is correct |
8 |
Correct |
50 ms |
5740 KB |
Output is correct |
9 |
Correct |
39 ms |
5740 KB |
Output is correct |
10 |
Correct |
39 ms |
5740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
2 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
2 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
2 ms |
2924 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2540 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
2 ms |
2668 KB |
Output is correct |
16 |
Correct |
2 ms |
2540 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
2 ms |
2540 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
2 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
2 ms |
2924 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2540 KB |
Output is correct |
14 |
Correct |
2 ms |
2796 KB |
Output is correct |
15 |
Correct |
2 ms |
2668 KB |
Output is correct |
16 |
Correct |
2 ms |
2540 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
2 ms |
2540 KB |
Output is correct |
20 |
Correct |
2 ms |
2796 KB |
Output is correct |
21 |
Correct |
12 ms |
22124 KB |
Output is correct |
22 |
Correct |
35 ms |
61164 KB |
Output is correct |
23 |
Correct |
15 ms |
27756 KB |
Output is correct |
24 |
Correct |
12 ms |
22636 KB |
Output is correct |
25 |
Correct |
16 ms |
25708 KB |
Output is correct |
26 |
Correct |
12 ms |
21868 KB |
Output is correct |
27 |
Correct |
16 ms |
25068 KB |
Output is correct |
28 |
Correct |
9 ms |
17388 KB |
Output is correct |
29 |
Correct |
9 ms |
17132 KB |
Output is correct |
30 |
Correct |
10 ms |
16108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
5484 KB |
Output is correct |
2 |
Correct |
35 ms |
5484 KB |
Output is correct |
3 |
Correct |
37 ms |
5612 KB |
Output is correct |
4 |
Correct |
35 ms |
5612 KB |
Output is correct |
5 |
Correct |
38 ms |
5740 KB |
Output is correct |
6 |
Correct |
35 ms |
5484 KB |
Output is correct |
7 |
Correct |
33 ms |
5484 KB |
Output is correct |
8 |
Correct |
35 ms |
5484 KB |
Output is correct |
9 |
Correct |
34 ms |
5484 KB |
Output is correct |
10 |
Correct |
38 ms |
5612 KB |
Output is correct |
11 |
Correct |
25 ms |
3820 KB |
Output is correct |
12 |
Correct |
24 ms |
3820 KB |
Output is correct |
13 |
Correct |
23 ms |
3692 KB |
Output is correct |
14 |
Correct |
23 ms |
3820 KB |
Output is correct |
15 |
Correct |
34 ms |
5484 KB |
Output is correct |
16 |
Correct |
23 ms |
3820 KB |
Output is correct |
17 |
Correct |
24 ms |
3948 KB |
Output is correct |
18 |
Correct |
37 ms |
5612 KB |
Output is correct |
19 |
Correct |
35 ms |
5356 KB |
Output is correct |
20 |
Correct |
23 ms |
3820 KB |
Output is correct |
21 |
Correct |
38 ms |
5740 KB |
Output is correct |
22 |
Correct |
37 ms |
5612 KB |
Output is correct |
23 |
Correct |
45 ms |
5740 KB |
Output is correct |
24 |
Correct |
41 ms |
5612 KB |
Output is correct |
25 |
Correct |
39 ms |
5740 KB |
Output is correct |
26 |
Correct |
39 ms |
5740 KB |
Output is correct |
27 |
Correct |
40 ms |
5740 KB |
Output is correct |
28 |
Correct |
50 ms |
5740 KB |
Output is correct |
29 |
Correct |
39 ms |
5740 KB |
Output is correct |
30 |
Correct |
39 ms |
5740 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
1004 KB |
Output is correct |
33 |
Correct |
1 ms |
1004 KB |
Output is correct |
34 |
Correct |
1 ms |
1004 KB |
Output is correct |
35 |
Correct |
2 ms |
1004 KB |
Output is correct |
36 |
Correct |
1 ms |
1004 KB |
Output is correct |
37 |
Correct |
1 ms |
1004 KB |
Output is correct |
38 |
Correct |
1 ms |
1004 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
1004 KB |
Output is correct |
41 |
Correct |
2 ms |
2924 KB |
Output is correct |
42 |
Correct |
2 ms |
2668 KB |
Output is correct |
43 |
Correct |
2 ms |
2540 KB |
Output is correct |
44 |
Correct |
2 ms |
2796 KB |
Output is correct |
45 |
Correct |
2 ms |
2668 KB |
Output is correct |
46 |
Correct |
2 ms |
2540 KB |
Output is correct |
47 |
Correct |
2 ms |
2796 KB |
Output is correct |
48 |
Correct |
2 ms |
2540 KB |
Output is correct |
49 |
Correct |
2 ms |
2540 KB |
Output is correct |
50 |
Correct |
2 ms |
2796 KB |
Output is correct |
51 |
Correct |
12 ms |
22124 KB |
Output is correct |
52 |
Correct |
35 ms |
61164 KB |
Output is correct |
53 |
Correct |
15 ms |
27756 KB |
Output is correct |
54 |
Correct |
12 ms |
22636 KB |
Output is correct |
55 |
Correct |
16 ms |
25708 KB |
Output is correct |
56 |
Correct |
12 ms |
21868 KB |
Output is correct |
57 |
Correct |
16 ms |
25068 KB |
Output is correct |
58 |
Correct |
9 ms |
17388 KB |
Output is correct |
59 |
Correct |
9 ms |
17132 KB |
Output is correct |
60 |
Correct |
10 ms |
16108 KB |
Output is correct |
61 |
Incorrect |
37 ms |
5612 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |