#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 9;
const int N2 = 5e3 + 9;
const int INF = 1e18 + 9;
int n, k, a[N], ps[N], f[N], pre[N2], dp[N2][N2];
namespace sub1 {
bool valid (){
for (int i = 1; i <= n; i++) if (a[i] < 0) return 0;
return 1;
}
void solve (){
int res = 0;
for (int i = 1; i <= n; i++) res += a[i];
cout << res;
}
}
namespace sub2 {
bool valid (){
int x = 0;
for (int i = 1; i <= n; i++){
if (a[i] < 0) x++;
if (x >= 2) break;
}
return x <= 1;
}
void solve (){
int sum1 = 0, sum2 = 0, sumall = 0;
bool flag = 0;
for (int i = 1; i <= n; i++){
if (a[i] < 0) flag = 1;
else if (flag == 0) sum1 += a[i];
else sum2 += a[i];
}
for (int i = 1; i <= n; i++) sumall += a[i];
if (k == 1) cout << max (sum1, max (sum2, sumall));
else cout << sum1 + sum2;
}
}
namespace sub3 {
bool valid (){
return k == 1;
}
void solve (){
for (int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i];
f[n] = ps[n];
for (int i = n - 1; i >= 1; i--) f[i] = max (f[i + 1], ps[i]);
int res = 0;
for (int i = 1; i <= n; i++) res = max (res, f[i] - ps[i - 1]);
cout << res;
}
}
namespace sub45 {
bool valid (){
return n <= 500;
}
void solve (){
dp[1][1] = a[1];
pre[1] = 0;
int res = 0;
for (int i = 2; i <= n; i++){
for (int j = 1; j <= k; j++){
if (j > i) continue;
dp[i][j] = max (dp[i][j], dp[i - 1][j] + a[i]);
for (int k = 1; k < i; k++) dp[i][j] = max (dp[i][j], dp[k][j - 1] + a[i]);
res = max (res, dp[i][j]);
}
}
cout << res;
}
}
void sub6 (){
dp[1][1] = a[1];
pre[1] = 0;
int res = 0;
for (int i = 2; i <= n; i++){
for (int j = 1; j <= k; j++){
if (j > i) continue;
dp[i][j] = max (dp[i][j], max (dp[i - 1][j] + a[i], pre[j - 1] + a[i]));
pre[j] = max (pre[j], dp[i][j]);
res = max (res, dp[i][j]);
}
}
cout << res;
}
signed main (){
ios_base::sync_with_stdio (false);
cin.tie (NULL);
cout.tie (NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
if (sub1::valid ()) sub1::solve ();
else if (sub2::valid ()) sub2::solve ();
else if (sub3::valid ()) sub3::solve ();
else if (sub45::valid ()) sub45::solve ();
else sub6 ();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4440 KB |
Output is correct |
2 |
Correct |
22 ms |
4440 KB |
Output is correct |
3 |
Correct |
22 ms |
4688 KB |
Output is correct |
4 |
Correct |
23 ms |
4696 KB |
Output is correct |
5 |
Correct |
25 ms |
4440 KB |
Output is correct |
6 |
Correct |
28 ms |
4560 KB |
Output is correct |
7 |
Correct |
22 ms |
4440 KB |
Output is correct |
8 |
Correct |
22 ms |
4560 KB |
Output is correct |
9 |
Correct |
22 ms |
4696 KB |
Output is correct |
10 |
Correct |
22 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4440 KB |
Output is correct |
2 |
Correct |
15 ms |
4700 KB |
Output is correct |
3 |
Correct |
15 ms |
4440 KB |
Output is correct |
4 |
Correct |
15 ms |
4444 KB |
Output is correct |
5 |
Correct |
23 ms |
4440 KB |
Output is correct |
6 |
Correct |
14 ms |
4440 KB |
Output is correct |
7 |
Correct |
15 ms |
4696 KB |
Output is correct |
8 |
Correct |
22 ms |
4440 KB |
Output is correct |
9 |
Correct |
28 ms |
4700 KB |
Output is correct |
10 |
Correct |
16 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
8528 KB |
Output is correct |
2 |
Correct |
27 ms |
8652 KB |
Output is correct |
3 |
Correct |
27 ms |
8532 KB |
Output is correct |
4 |
Correct |
27 ms |
8648 KB |
Output is correct |
5 |
Correct |
29 ms |
8528 KB |
Output is correct |
6 |
Correct |
27 ms |
8536 KB |
Output is correct |
7 |
Correct |
27 ms |
8784 KB |
Output is correct |
8 |
Correct |
26 ms |
8528 KB |
Output is correct |
9 |
Correct |
29 ms |
8784 KB |
Output is correct |
10 |
Correct |
27 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
5720 KB |
Output is correct |
12 |
Correct |
3 ms |
5720 KB |
Output is correct |
13 |
Correct |
2 ms |
5464 KB |
Output is correct |
14 |
Correct |
4 ms |
5720 KB |
Output is correct |
15 |
Correct |
2 ms |
5720 KB |
Output is correct |
16 |
Correct |
2 ms |
5464 KB |
Output is correct |
17 |
Correct |
3 ms |
5720 KB |
Output is correct |
18 |
Correct |
1 ms |
5464 KB |
Output is correct |
19 |
Correct |
2 ms |
5464 KB |
Output is correct |
20 |
Correct |
2 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
5720 KB |
Output is correct |
12 |
Correct |
3 ms |
5720 KB |
Output is correct |
13 |
Correct |
2 ms |
5464 KB |
Output is correct |
14 |
Correct |
4 ms |
5720 KB |
Output is correct |
15 |
Correct |
2 ms |
5720 KB |
Output is correct |
16 |
Correct |
2 ms |
5464 KB |
Output is correct |
17 |
Correct |
3 ms |
5720 KB |
Output is correct |
18 |
Correct |
1 ms |
5464 KB |
Output is correct |
19 |
Correct |
2 ms |
5464 KB |
Output is correct |
20 |
Correct |
2 ms |
5720 KB |
Output is correct |
21 |
Incorrect |
7 ms |
15192 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4440 KB |
Output is correct |
2 |
Correct |
22 ms |
4440 KB |
Output is correct |
3 |
Correct |
22 ms |
4688 KB |
Output is correct |
4 |
Correct |
23 ms |
4696 KB |
Output is correct |
5 |
Correct |
25 ms |
4440 KB |
Output is correct |
6 |
Correct |
28 ms |
4560 KB |
Output is correct |
7 |
Correct |
22 ms |
4440 KB |
Output is correct |
8 |
Correct |
22 ms |
4560 KB |
Output is correct |
9 |
Correct |
22 ms |
4696 KB |
Output is correct |
10 |
Correct |
22 ms |
4440 KB |
Output is correct |
11 |
Correct |
14 ms |
4440 KB |
Output is correct |
12 |
Correct |
15 ms |
4700 KB |
Output is correct |
13 |
Correct |
15 ms |
4440 KB |
Output is correct |
14 |
Correct |
15 ms |
4444 KB |
Output is correct |
15 |
Correct |
23 ms |
4440 KB |
Output is correct |
16 |
Correct |
14 ms |
4440 KB |
Output is correct |
17 |
Correct |
15 ms |
4696 KB |
Output is correct |
18 |
Correct |
22 ms |
4440 KB |
Output is correct |
19 |
Correct |
28 ms |
4700 KB |
Output is correct |
20 |
Correct |
16 ms |
4696 KB |
Output is correct |
21 |
Correct |
28 ms |
8528 KB |
Output is correct |
22 |
Correct |
27 ms |
8652 KB |
Output is correct |
23 |
Correct |
27 ms |
8532 KB |
Output is correct |
24 |
Correct |
27 ms |
8648 KB |
Output is correct |
25 |
Correct |
29 ms |
8528 KB |
Output is correct |
26 |
Correct |
27 ms |
8536 KB |
Output is correct |
27 |
Correct |
27 ms |
8784 KB |
Output is correct |
28 |
Correct |
26 ms |
8528 KB |
Output is correct |
29 |
Correct |
29 ms |
8784 KB |
Output is correct |
30 |
Correct |
27 ms |
8532 KB |
Output is correct |
31 |
Correct |
1 ms |
4440 KB |
Output is correct |
32 |
Correct |
1 ms |
4696 KB |
Output is correct |
33 |
Correct |
1 ms |
4696 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4696 KB |
Output is correct |
36 |
Correct |
1 ms |
4696 KB |
Output is correct |
37 |
Correct |
1 ms |
4696 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
2 ms |
4440 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
5720 KB |
Output is correct |
42 |
Correct |
3 ms |
5720 KB |
Output is correct |
43 |
Correct |
2 ms |
5464 KB |
Output is correct |
44 |
Correct |
4 ms |
5720 KB |
Output is correct |
45 |
Correct |
2 ms |
5720 KB |
Output is correct |
46 |
Correct |
2 ms |
5464 KB |
Output is correct |
47 |
Correct |
3 ms |
5720 KB |
Output is correct |
48 |
Correct |
1 ms |
5464 KB |
Output is correct |
49 |
Correct |
2 ms |
5464 KB |
Output is correct |
50 |
Correct |
2 ms |
5720 KB |
Output is correct |
51 |
Incorrect |
7 ms |
15192 KB |
Output isn't correct |
52 |
Halted |
0 ms |
0 KB |
- |