// #define MINHDEPTRAI "/Volumes/icebear/source_code/VS_code"
#ifdef MINHDEPTRAI
#include "/Library/Developer/CommandLineTools/usr/include/c++/v1/bits/stdc++.h"
#include <chrono>
#define __gcd(a, b) gcd(a, b)
using namespace std ::chrono;
#else
#include <bits/stdc++.h>
#endif
using namespace std;
#define foru(i, a, b) for (int i = a; i <= b; ++i)
#define ford(i, a, b) for (int i = a; i >= b; --i)
#define forr(i, l, r) for (int i = (l); i < (r); ++i)
#define IOS cin.tie(0),cout.tie(0)->sync_with_stdio(0);
#define mp(a, b) make_pair(a, b)
#define int long long
typedef pair<int, int> pii;
typedef pair<pair<int, int>, int> piii;
#define endl '\n'
using ld = long double;
const string name_minh = "maxcross";
const int maxN = 2e3 + 5;
const int maxK = 3e5 + 5;
const int mod = 1e9 + 7;
const long long inf = 1e18;
int dp[maxN][maxN][2], cnt_negative;
int n, k;
int arr[maxK], prefix_sum[maxK];
void k_1(){
int current = 0, best = -inf;
foru(i, 1, n){
current = max(arr[i], current + arr[i]);
best = max(best, current);
}
cout << best;
}
void solve(){
cin >> n >> k;
foru(i, 1, n) {
cin >> arr[i];
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
if(arr[i] < 0) cnt_negative++;
}
if(k == 1){
k_1();
return;
}
else if(cnt_negative == 0){
cout << prefix_sum[n] << endl;
}
else if(cnt_negative == 1){
foru(i, 1, n){
if(arr[i] < 0){
int beggin = prefix_sum[i - 1];
int endd = prefix_sum[n] - prefix_sum[i];
if(k >= 2) cout << beggin + endd << endl;
else cout << max({beggin, endd, beggin + endd + arr[i]});
break;
}
}
}
else {
foru(i, 1, n){
foru(j, 1, k){
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][1], dp[i - 1][j][1], dp[i - 1][j - 1][0]}) + arr[i];
}
}
cout << max(dp[n][k][0], dp[n][k][1]) << endl;
}
}
signed main() {
IOS
// #ifndef MINHDEPTRAI
// freopen((name_minh + ".in").c_str(), "r", stdin);
// freopen((name_minh + ".out").c_str(), "w", stdout);
// #endif
int t = 1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
4800 KB |
Output is correct |
2 |
Correct |
26 ms |
5488 KB |
Output is correct |
3 |
Correct |
28 ms |
5552 KB |
Output is correct |
4 |
Correct |
26 ms |
5584 KB |
Output is correct |
5 |
Correct |
25 ms |
5580 KB |
Output is correct |
6 |
Correct |
25 ms |
5456 KB |
Output is correct |
7 |
Correct |
26 ms |
5408 KB |
Output is correct |
8 |
Correct |
26 ms |
5540 KB |
Output is correct |
9 |
Correct |
25 ms |
5412 KB |
Output is correct |
10 |
Correct |
26 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
4820 KB |
Output is correct |
2 |
Correct |
17 ms |
5220 KB |
Output is correct |
3 |
Correct |
16 ms |
5180 KB |
Output is correct |
4 |
Correct |
16 ms |
5156 KB |
Output is correct |
5 |
Correct |
31 ms |
5476 KB |
Output is correct |
6 |
Correct |
19 ms |
5216 KB |
Output is correct |
7 |
Correct |
16 ms |
5268 KB |
Output is correct |
8 |
Correct |
34 ms |
5580 KB |
Output is correct |
9 |
Correct |
28 ms |
5648 KB |
Output is correct |
10 |
Correct |
16 ms |
5188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
5032 KB |
Output is correct |
2 |
Correct |
30 ms |
5436 KB |
Output is correct |
3 |
Correct |
29 ms |
5508 KB |
Output is correct |
4 |
Correct |
27 ms |
5504 KB |
Output is correct |
5 |
Correct |
27 ms |
5584 KB |
Output is correct |
6 |
Correct |
30 ms |
5616 KB |
Output is correct |
7 |
Correct |
35 ms |
5564 KB |
Output is correct |
8 |
Correct |
31 ms |
5504 KB |
Output is correct |
9 |
Correct |
28 ms |
5540 KB |
Output is correct |
10 |
Correct |
28 ms |
5636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1620 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1620 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
6 ms |
14212 KB |
Output is correct |
22 |
Correct |
23 ms |
59068 KB |
Output is correct |
23 |
Correct |
8 ms |
19924 KB |
Output is correct |
24 |
Correct |
6 ms |
14676 KB |
Output is correct |
25 |
Correct |
8 ms |
17492 KB |
Output is correct |
26 |
Correct |
6 ms |
13780 KB |
Output is correct |
27 |
Correct |
8 ms |
16980 KB |
Output is correct |
28 |
Correct |
4 ms |
9300 KB |
Output is correct |
29 |
Correct |
3 ms |
9172 KB |
Output is correct |
30 |
Correct |
4 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
4800 KB |
Output is correct |
2 |
Correct |
26 ms |
5488 KB |
Output is correct |
3 |
Correct |
28 ms |
5552 KB |
Output is correct |
4 |
Correct |
26 ms |
5584 KB |
Output is correct |
5 |
Correct |
25 ms |
5580 KB |
Output is correct |
6 |
Correct |
25 ms |
5456 KB |
Output is correct |
7 |
Correct |
26 ms |
5408 KB |
Output is correct |
8 |
Correct |
26 ms |
5540 KB |
Output is correct |
9 |
Correct |
25 ms |
5412 KB |
Output is correct |
10 |
Correct |
26 ms |
5580 KB |
Output is correct |
11 |
Correct |
16 ms |
4820 KB |
Output is correct |
12 |
Correct |
17 ms |
5220 KB |
Output is correct |
13 |
Correct |
16 ms |
5180 KB |
Output is correct |
14 |
Correct |
16 ms |
5156 KB |
Output is correct |
15 |
Correct |
31 ms |
5476 KB |
Output is correct |
16 |
Correct |
19 ms |
5216 KB |
Output is correct |
17 |
Correct |
16 ms |
5268 KB |
Output is correct |
18 |
Correct |
34 ms |
5580 KB |
Output is correct |
19 |
Correct |
28 ms |
5648 KB |
Output is correct |
20 |
Correct |
16 ms |
5188 KB |
Output is correct |
21 |
Correct |
27 ms |
5032 KB |
Output is correct |
22 |
Correct |
30 ms |
5436 KB |
Output is correct |
23 |
Correct |
29 ms |
5508 KB |
Output is correct |
24 |
Correct |
27 ms |
5504 KB |
Output is correct |
25 |
Correct |
27 ms |
5584 KB |
Output is correct |
26 |
Correct |
30 ms |
5616 KB |
Output is correct |
27 |
Correct |
35 ms |
5564 KB |
Output is correct |
28 |
Correct |
31 ms |
5504 KB |
Output is correct |
29 |
Correct |
28 ms |
5540 KB |
Output is correct |
30 |
Correct |
28 ms |
5636 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
596 KB |
Output is correct |
33 |
Correct |
0 ms |
596 KB |
Output is correct |
34 |
Correct |
0 ms |
596 KB |
Output is correct |
35 |
Correct |
0 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
0 ms |
596 KB |
Output is correct |
38 |
Correct |
0 ms |
596 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
1620 KB |
Output is correct |
42 |
Correct |
1 ms |
1620 KB |
Output is correct |
43 |
Correct |
1 ms |
1492 KB |
Output is correct |
44 |
Correct |
1 ms |
1620 KB |
Output is correct |
45 |
Correct |
1 ms |
1492 KB |
Output is correct |
46 |
Correct |
1 ms |
1364 KB |
Output is correct |
47 |
Correct |
1 ms |
1620 KB |
Output is correct |
48 |
Correct |
1 ms |
1364 KB |
Output is correct |
49 |
Correct |
1 ms |
1492 KB |
Output is correct |
50 |
Correct |
1 ms |
1492 KB |
Output is correct |
51 |
Correct |
6 ms |
14212 KB |
Output is correct |
52 |
Correct |
23 ms |
59068 KB |
Output is correct |
53 |
Correct |
8 ms |
19924 KB |
Output is correct |
54 |
Correct |
6 ms |
14676 KB |
Output is correct |
55 |
Correct |
8 ms |
17492 KB |
Output is correct |
56 |
Correct |
6 ms |
13780 KB |
Output is correct |
57 |
Correct |
8 ms |
16980 KB |
Output is correct |
58 |
Correct |
4 ms |
9300 KB |
Output is correct |
59 |
Correct |
3 ms |
9172 KB |
Output is correct |
60 |
Correct |
4 ms |
8276 KB |
Output is correct |
61 |
Runtime error |
261 ms |
137856 KB |
Execution killed with signal 11 |
62 |
Halted |
0 ms |
0 KB |
- |