#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define ld long double
using namespace std;
void data() {
#ifdef NURS
freopen("main.in", "r", stdin);
freopen("main.out", "w", stdout);
#endif
}
void win()
{
ios_base::sync_with_stdio(0),
cin.tie(0),cout.tie(0);
}
int block = 400;
const ld eps = 1e-6;
const int N = 1e6 + 500;
const int mod = 1e9 + 7;
const ll hh = 100010683;
const ll hh2 = 150005819;
int n, k, g, pos;
int a[N];
ll s, ans;
ll dp[5000][5000], dp2[5000][5000], pref[5000];
int main()
{
data();
win();
cin >> n >> k;
bool was = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
was = was | (a[i] < 0), s += a[i];
if (a[i] < 0)
g++, pos = i;
pref[i] += pref[i - 1] + a[i];
}
//first subtask
if (was == 0)
{
cout << s << '\n';
return 0;
}
//second subtask
if (g == 1 && k >= 2)
{
cout << s - a[pos] << '\n';
return 0;
}
else if (g == 1 && k == 1)
{
ans = max(ans, s);
s = 0;
for (int i = 1; i < pos; i++)
s += a[i];
ans = max(ans, s), s = 0;
for (int i = pos + 1; i <= n; i++)
s += a[i];
ans = max(ans, s);
cout << ans << '\n';
return 0;
}
//3 subtask
ll mn = 0;
for (int i = 1; i <= n; i++)
{
ans = max(ans, pref[i] - mn);
if (i == 1)
mn = pref[i];
else
mn = min(mn, pref[i]);
}
if (k == 1)
{
cout << ans;
return 0;
}
//4, 5 subtasks
if (n <= 300)
{
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= k; j++)
{
dp[i][j] = -1e9;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
for (int len = 0; len <= i; len++)
{
if (dp[i - len][j - 1] != -1e9)
{
dp[i][j] = max(dp[i - len][j - 1] + pref[i] - pref[i - len], dp[i][j]);
}
}
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
if (i == n && j == k)
ans = max(ans, dp[i][j]);
}
dp[i][0] = max(dp[i][0], dp[i - 1][0]);
}
cout << ans;
}
else
{
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= k; j++)
{
dp[i][j] = -1e9, dp2[i][j] = -1e9;;
}
}
dp[0][0] = 0, dp2[0][0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i]);
dp[i][j] = max(dp[i][j], dp2[i - 1][j - 1] + a[i]);
dp2[i][j] = max(dp2[i - 1][j], dp2[i][j]);
dp2[i][j] = max(dp2[i][j], dp[i][j]);
}
}
cout << dp2[n][k];
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
3820 KB |
Output is correct |
2 |
Correct |
35 ms |
3820 KB |
Output is correct |
3 |
Correct |
35 ms |
3820 KB |
Output is correct |
4 |
Correct |
41 ms |
3820 KB |
Output is correct |
5 |
Correct |
35 ms |
3820 KB |
Output is correct |
6 |
Correct |
36 ms |
3820 KB |
Output is correct |
7 |
Correct |
34 ms |
3820 KB |
Output is correct |
8 |
Correct |
35 ms |
3820 KB |
Output is correct |
9 |
Correct |
34 ms |
3820 KB |
Output is correct |
10 |
Correct |
34 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3820 KB |
Output is correct |
2 |
Correct |
27 ms |
3948 KB |
Output is correct |
3 |
Correct |
23 ms |
3820 KB |
Output is correct |
4 |
Correct |
24 ms |
3820 KB |
Output is correct |
5 |
Correct |
36 ms |
3820 KB |
Output is correct |
6 |
Correct |
24 ms |
3820 KB |
Output is correct |
7 |
Correct |
24 ms |
3820 KB |
Output is correct |
8 |
Correct |
40 ms |
3820 KB |
Output is correct |
9 |
Correct |
36 ms |
3692 KB |
Output is correct |
10 |
Correct |
31 ms |
3824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3804 KB |
Output is correct |
2 |
Correct |
40 ms |
3820 KB |
Output is correct |
3 |
Correct |
40 ms |
3820 KB |
Output is correct |
4 |
Correct |
41 ms |
3820 KB |
Output is correct |
5 |
Correct |
44 ms |
3820 KB |
Output is correct |
6 |
Correct |
40 ms |
3820 KB |
Output is correct |
7 |
Correct |
47 ms |
3948 KB |
Output is correct |
8 |
Correct |
47 ms |
3820 KB |
Output is correct |
9 |
Correct |
40 ms |
3820 KB |
Output is correct |
10 |
Correct |
40 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
6 ms |
1644 KB |
Output is correct |
12 |
Correct |
8 ms |
1516 KB |
Output is correct |
13 |
Correct |
3 ms |
1516 KB |
Output is correct |
14 |
Correct |
6 ms |
1516 KB |
Output is correct |
15 |
Correct |
4 ms |
1516 KB |
Output is correct |
16 |
Correct |
3 ms |
1516 KB |
Output is correct |
17 |
Correct |
8 ms |
1644 KB |
Output is correct |
18 |
Correct |
1 ms |
1516 KB |
Output is correct |
19 |
Correct |
2 ms |
1516 KB |
Output is correct |
20 |
Correct |
2 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
6 ms |
1644 KB |
Output is correct |
12 |
Correct |
8 ms |
1516 KB |
Output is correct |
13 |
Correct |
3 ms |
1516 KB |
Output is correct |
14 |
Correct |
6 ms |
1516 KB |
Output is correct |
15 |
Correct |
4 ms |
1516 KB |
Output is correct |
16 |
Correct |
3 ms |
1516 KB |
Output is correct |
17 |
Correct |
8 ms |
1644 KB |
Output is correct |
18 |
Correct |
1 ms |
1516 KB |
Output is correct |
19 |
Correct |
2 ms |
1516 KB |
Output is correct |
20 |
Correct |
2 ms |
1516 KB |
Output is correct |
21 |
Incorrect |
13 ms |
22252 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
3820 KB |
Output is correct |
2 |
Correct |
35 ms |
3820 KB |
Output is correct |
3 |
Correct |
35 ms |
3820 KB |
Output is correct |
4 |
Correct |
41 ms |
3820 KB |
Output is correct |
5 |
Correct |
35 ms |
3820 KB |
Output is correct |
6 |
Correct |
36 ms |
3820 KB |
Output is correct |
7 |
Correct |
34 ms |
3820 KB |
Output is correct |
8 |
Correct |
35 ms |
3820 KB |
Output is correct |
9 |
Correct |
34 ms |
3820 KB |
Output is correct |
10 |
Correct |
34 ms |
3820 KB |
Output is correct |
11 |
Correct |
24 ms |
3820 KB |
Output is correct |
12 |
Correct |
27 ms |
3948 KB |
Output is correct |
13 |
Correct |
23 ms |
3820 KB |
Output is correct |
14 |
Correct |
24 ms |
3820 KB |
Output is correct |
15 |
Correct |
36 ms |
3820 KB |
Output is correct |
16 |
Correct |
24 ms |
3820 KB |
Output is correct |
17 |
Correct |
24 ms |
3820 KB |
Output is correct |
18 |
Correct |
40 ms |
3820 KB |
Output is correct |
19 |
Correct |
36 ms |
3692 KB |
Output is correct |
20 |
Correct |
31 ms |
3824 KB |
Output is correct |
21 |
Correct |
39 ms |
3804 KB |
Output is correct |
22 |
Correct |
40 ms |
3820 KB |
Output is correct |
23 |
Correct |
40 ms |
3820 KB |
Output is correct |
24 |
Correct |
41 ms |
3820 KB |
Output is correct |
25 |
Correct |
44 ms |
3820 KB |
Output is correct |
26 |
Correct |
40 ms |
3820 KB |
Output is correct |
27 |
Correct |
47 ms |
3948 KB |
Output is correct |
28 |
Correct |
47 ms |
3820 KB |
Output is correct |
29 |
Correct |
40 ms |
3820 KB |
Output is correct |
30 |
Correct |
40 ms |
3820 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
748 KB |
Output is correct |
33 |
Correct |
1 ms |
620 KB |
Output is correct |
34 |
Correct |
1 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
1 ms |
620 KB |
Output is correct |
38 |
Correct |
1 ms |
620 KB |
Output is correct |
39 |
Correct |
0 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
620 KB |
Output is correct |
41 |
Correct |
6 ms |
1644 KB |
Output is correct |
42 |
Correct |
8 ms |
1516 KB |
Output is correct |
43 |
Correct |
3 ms |
1516 KB |
Output is correct |
44 |
Correct |
6 ms |
1516 KB |
Output is correct |
45 |
Correct |
4 ms |
1516 KB |
Output is correct |
46 |
Correct |
3 ms |
1516 KB |
Output is correct |
47 |
Correct |
8 ms |
1644 KB |
Output is correct |
48 |
Correct |
1 ms |
1516 KB |
Output is correct |
49 |
Correct |
2 ms |
1516 KB |
Output is correct |
50 |
Correct |
2 ms |
1516 KB |
Output is correct |
51 |
Incorrect |
13 ms |
22252 KB |
Output isn't correct |
52 |
Halted |
0 ms |
0 KB |
- |