#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n, k;
vi arr;
ll dp[2010][2010][2];
ll f(int at, int segments, int taken){
if(at >= n)
return 0;
if(segments > k)
return 0;
if(dp[at][segments][taken] != -1)
return dp[at][segments][taken];
ll best = 0;
best = max(best, arr[at] + f(at + 1, segments, 1));
if(taken == 0)
best = max(best, f(at + 1, segments, 0));
if(segments + 1 < k && taken == 1)
best = max(best, f(at + 1, segments + 1, 0));
return dp[at][segments][taken] = best;
}
int main(){
cin>>n>>k;
arr.resize(n);
int cnt = 0;
for(int i = 0; i < n; i++){
cin>>arr[i];
if(arr[i] < 0) cnt++;
}
if(cnt == 0){
ll ans = 0;
for(int i = 0; i < n; i++) ans += arr[i];
cout<<ans<<endl;
}
else if(cnt == 1){
int left = 0, right = 0, flag = 0, pos = -1;
for(int i = 0; i < n; i++){
if(arr[i] < 0){
pos = i;
flag = 1;
}
else if(flag == 0) left += arr[i];
else right += arr[i];
}
cout<<max(max(left, right), left + right + arr[pos]);
}
else if(k == 1){
ll ans = 0, help = 0;
for(int i = 0; i < n; i++){
help += arr[i];
if(ans < help) ans = help;
if(help < 0) help = 0;
}
cout<<ans<<endl;
}
else{
memset(dp, -1, sizeof dp);
cout<<f(0, 0, 0)<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
1484 KB |
Output is correct |
2 |
Correct |
110 ms |
1364 KB |
Output is correct |
3 |
Correct |
107 ms |
1468 KB |
Output is correct |
4 |
Correct |
110 ms |
1460 KB |
Output is correct |
5 |
Correct |
110 ms |
1452 KB |
Output is correct |
6 |
Correct |
128 ms |
1436 KB |
Output is correct |
7 |
Correct |
103 ms |
1364 KB |
Output is correct |
8 |
Correct |
106 ms |
1364 KB |
Output is correct |
9 |
Correct |
108 ms |
1436 KB |
Output is correct |
10 |
Correct |
106 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
1444 KB |
Output is correct |
2 |
Correct |
59 ms |
2496 KB |
Output is correct |
3 |
Correct |
61 ms |
2464 KB |
Output is correct |
4 |
Incorrect |
64 ms |
2464 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
1464 KB |
Output is correct |
2 |
Correct |
116 ms |
1364 KB |
Output is correct |
3 |
Correct |
113 ms |
1452 KB |
Output is correct |
4 |
Correct |
116 ms |
1440 KB |
Output is correct |
5 |
Correct |
113 ms |
1364 KB |
Output is correct |
6 |
Correct |
116 ms |
1456 KB |
Output is correct |
7 |
Correct |
114 ms |
1468 KB |
Output is correct |
8 |
Correct |
114 ms |
1456 KB |
Output is correct |
9 |
Correct |
114 ms |
1464 KB |
Output is correct |
10 |
Correct |
112 ms |
1464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
27 ms |
63444 KB |
Output is correct |
3 |
Correct |
25 ms |
63472 KB |
Output is correct |
4 |
Correct |
25 ms |
63428 KB |
Output is correct |
5 |
Correct |
25 ms |
63532 KB |
Output is correct |
6 |
Correct |
24 ms |
63444 KB |
Output is correct |
7 |
Correct |
25 ms |
63536 KB |
Output is correct |
8 |
Correct |
26 ms |
63420 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
30 ms |
63448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
27 ms |
63444 KB |
Output is correct |
3 |
Correct |
25 ms |
63472 KB |
Output is correct |
4 |
Correct |
25 ms |
63428 KB |
Output is correct |
5 |
Correct |
25 ms |
63532 KB |
Output is correct |
6 |
Correct |
24 ms |
63444 KB |
Output is correct |
7 |
Correct |
25 ms |
63536 KB |
Output is correct |
8 |
Correct |
26 ms |
63420 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
30 ms |
63448 KB |
Output is correct |
11 |
Correct |
26 ms |
63488 KB |
Output is correct |
12 |
Correct |
26 ms |
63480 KB |
Output is correct |
13 |
Correct |
26 ms |
63436 KB |
Output is correct |
14 |
Correct |
26 ms |
63516 KB |
Output is correct |
15 |
Correct |
26 ms |
63444 KB |
Output is correct |
16 |
Correct |
26 ms |
63544 KB |
Output is correct |
17 |
Correct |
25 ms |
63556 KB |
Output is correct |
18 |
Correct |
25 ms |
63444 KB |
Output is correct |
19 |
Correct |
26 ms |
63492 KB |
Output is correct |
20 |
Correct |
25 ms |
63444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
27 ms |
63444 KB |
Output is correct |
3 |
Correct |
25 ms |
63472 KB |
Output is correct |
4 |
Correct |
25 ms |
63428 KB |
Output is correct |
5 |
Correct |
25 ms |
63532 KB |
Output is correct |
6 |
Correct |
24 ms |
63444 KB |
Output is correct |
7 |
Correct |
25 ms |
63536 KB |
Output is correct |
8 |
Correct |
26 ms |
63420 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
30 ms |
63448 KB |
Output is correct |
11 |
Correct |
26 ms |
63488 KB |
Output is correct |
12 |
Correct |
26 ms |
63480 KB |
Output is correct |
13 |
Correct |
26 ms |
63436 KB |
Output is correct |
14 |
Correct |
26 ms |
63516 KB |
Output is correct |
15 |
Correct |
26 ms |
63444 KB |
Output is correct |
16 |
Correct |
26 ms |
63544 KB |
Output is correct |
17 |
Correct |
25 ms |
63556 KB |
Output is correct |
18 |
Correct |
25 ms |
63444 KB |
Output is correct |
19 |
Correct |
26 ms |
63492 KB |
Output is correct |
20 |
Correct |
25 ms |
63444 KB |
Output is correct |
21 |
Correct |
38 ms |
63572 KB |
Output is correct |
22 |
Correct |
64 ms |
63688 KB |
Output is correct |
23 |
Correct |
50 ms |
63644 KB |
Output is correct |
24 |
Correct |
43 ms |
63568 KB |
Output is correct |
25 |
Correct |
49 ms |
63788 KB |
Output is correct |
26 |
Correct |
38 ms |
63692 KB |
Output is correct |
27 |
Correct |
43 ms |
63668 KB |
Output is correct |
28 |
Correct |
28 ms |
63668 KB |
Output is correct |
29 |
Correct |
28 ms |
63700 KB |
Output is correct |
30 |
Correct |
26 ms |
63600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
1484 KB |
Output is correct |
2 |
Correct |
110 ms |
1364 KB |
Output is correct |
3 |
Correct |
107 ms |
1468 KB |
Output is correct |
4 |
Correct |
110 ms |
1460 KB |
Output is correct |
5 |
Correct |
110 ms |
1452 KB |
Output is correct |
6 |
Correct |
128 ms |
1436 KB |
Output is correct |
7 |
Correct |
103 ms |
1364 KB |
Output is correct |
8 |
Correct |
106 ms |
1364 KB |
Output is correct |
9 |
Correct |
108 ms |
1436 KB |
Output is correct |
10 |
Correct |
106 ms |
1364 KB |
Output is correct |
11 |
Correct |
55 ms |
1444 KB |
Output is correct |
12 |
Correct |
59 ms |
2496 KB |
Output is correct |
13 |
Correct |
61 ms |
2464 KB |
Output is correct |
14 |
Incorrect |
64 ms |
2464 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |