#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
const int MAXN = 3e5 + 5;
const long long INF = 1e18 + 7;
int N, K, a[MAXN];
struct SUB1{
vector <vector <vector <long long>>> dp;
void solve(void) {
dp.assign(N + 2, vector <vector <long long>> (K + 2, vector <long long> (3, -INF)));
dp[0][0][0] = 0;
FOR(i, 0, N - 1) FOR(j, 0, K) if (dp[i][j][0] != -INF || dp[i][j][1] != INF) {
dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1]);
dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1] + a[i + 1]);
dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j][0] + a[i + 1]);
}
long long ans = 0;
FOR(i, N, N) FOR(j, 0, K) REP(c, 2) ans = max(ans, dp[i][j][c]);
cout << ans; exit(0);
}
} sub1;
struct SUB2{
#define ii pair <int, int>
#define fi first
#define se second
long long sum[MAXN];
int M = 0, root[MAXN];
ii b[MAXN];
void solve(void) {
FOR(i, 1, N) sum[i] = sum[i - 1] + a[i];
FOR(i, 1, N) {
if (a[i] < 0) continue;
int l = i;
while (l <= N && a[l] >= 0) l++;
b[++M] = {i, l - 1};
i = l - 1;
}
if (M <= K) {
long long ans = 0;
FOR(i, 1, M) ans += sum[b[i].se] - sum[b[i].fi - 1];
cout << ans; exit(0);
}
long long ans = sum[N];
FOR(i, 1, M) ans = max(ans, sum[b[i].se] - sum[b[i].fi - 1]);
cout << ans;
}
} sub2;
void solve(void) {
cin >> N >> K;
FOR(i, 1, N) cin >> a[i];
if (1ll * N * K <= 1e7) sub1.solve();
sub2.solve();
}
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
//file("TASK");
//int T; cin >> T; while (T--)
solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
/*
6 2
1 2 3 -10 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
8780 KB |
Output is correct |
2 |
Correct |
31 ms |
8964 KB |
Output is correct |
3 |
Correct |
32 ms |
9000 KB |
Output is correct |
4 |
Correct |
31 ms |
8996 KB |
Output is correct |
5 |
Correct |
31 ms |
8916 KB |
Output is correct |
6 |
Correct |
38 ms |
8768 KB |
Output is correct |
7 |
Correct |
30 ms |
8848 KB |
Output is correct |
8 |
Correct |
31 ms |
8984 KB |
Output is correct |
9 |
Correct |
31 ms |
8768 KB |
Output is correct |
10 |
Correct |
262 ms |
211824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
62196 KB |
Output is correct |
2 |
Correct |
101 ms |
63480 KB |
Output is correct |
3 |
Correct |
90 ms |
62052 KB |
Output is correct |
4 |
Correct |
21 ms |
7248 KB |
Output is correct |
5 |
Correct |
97 ms |
63512 KB |
Output is correct |
6 |
Correct |
89 ms |
62052 KB |
Output is correct |
7 |
Correct |
21 ms |
7260 KB |
Output is correct |
8 |
Correct |
100 ms |
64912 KB |
Output is correct |
9 |
Correct |
102 ms |
63320 KB |
Output is correct |
10 |
Correct |
20 ms |
7288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
64844 KB |
Output is correct |
2 |
Correct |
105 ms |
64076 KB |
Output is correct |
3 |
Correct |
101 ms |
64588 KB |
Output is correct |
4 |
Correct |
101 ms |
64004 KB |
Output is correct |
5 |
Correct |
103 ms |
64584 KB |
Output is correct |
6 |
Correct |
109 ms |
65040 KB |
Output is correct |
7 |
Correct |
104 ms |
65252 KB |
Output is correct |
8 |
Correct |
103 ms |
64608 KB |
Output is correct |
9 |
Correct |
104 ms |
65456 KB |
Output is correct |
10 |
Correct |
106 ms |
65224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2944 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2944 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3028 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
2 ms |
3072 KB |
Output is correct |
17 |
Correct |
2 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2944 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3028 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
2 ms |
3072 KB |
Output is correct |
17 |
Correct |
2 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
27 ms |
24196 KB |
Output is correct |
22 |
Correct |
215 ms |
181964 KB |
Output is correct |
23 |
Correct |
49 ms |
44108 KB |
Output is correct |
24 |
Correct |
29 ms |
25940 KB |
Output is correct |
25 |
Correct |
40 ms |
34984 KB |
Output is correct |
26 |
Correct |
24 ms |
21972 KB |
Output is correct |
27 |
Correct |
38 ms |
33176 KB |
Output is correct |
28 |
Correct |
6 ms |
6408 KB |
Output is correct |
29 |
Correct |
5 ms |
6028 KB |
Output is correct |
30 |
Correct |
2 ms |
3212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
8780 KB |
Output is correct |
2 |
Correct |
31 ms |
8964 KB |
Output is correct |
3 |
Correct |
32 ms |
9000 KB |
Output is correct |
4 |
Correct |
31 ms |
8996 KB |
Output is correct |
5 |
Correct |
31 ms |
8916 KB |
Output is correct |
6 |
Correct |
38 ms |
8768 KB |
Output is correct |
7 |
Correct |
30 ms |
8848 KB |
Output is correct |
8 |
Correct |
31 ms |
8984 KB |
Output is correct |
9 |
Correct |
31 ms |
8768 KB |
Output is correct |
10 |
Correct |
262 ms |
211824 KB |
Output is correct |
11 |
Correct |
94 ms |
62196 KB |
Output is correct |
12 |
Correct |
101 ms |
63480 KB |
Output is correct |
13 |
Correct |
90 ms |
62052 KB |
Output is correct |
14 |
Correct |
21 ms |
7248 KB |
Output is correct |
15 |
Correct |
97 ms |
63512 KB |
Output is correct |
16 |
Correct |
89 ms |
62052 KB |
Output is correct |
17 |
Correct |
21 ms |
7260 KB |
Output is correct |
18 |
Correct |
100 ms |
64912 KB |
Output is correct |
19 |
Correct |
102 ms |
63320 KB |
Output is correct |
20 |
Correct |
20 ms |
7288 KB |
Output is correct |
21 |
Correct |
103 ms |
64844 KB |
Output is correct |
22 |
Correct |
105 ms |
64076 KB |
Output is correct |
23 |
Correct |
101 ms |
64588 KB |
Output is correct |
24 |
Correct |
101 ms |
64004 KB |
Output is correct |
25 |
Correct |
103 ms |
64584 KB |
Output is correct |
26 |
Correct |
109 ms |
65040 KB |
Output is correct |
27 |
Correct |
104 ms |
65252 KB |
Output is correct |
28 |
Correct |
103 ms |
64608 KB |
Output is correct |
29 |
Correct |
104 ms |
65456 KB |
Output is correct |
30 |
Correct |
106 ms |
65224 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2944 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
1 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
2644 KB |
Output is correct |
37 |
Correct |
1 ms |
2644 KB |
Output is correct |
38 |
Correct |
1 ms |
2644 KB |
Output is correct |
39 |
Correct |
1 ms |
2644 KB |
Output is correct |
40 |
Correct |
1 ms |
2644 KB |
Output is correct |
41 |
Correct |
2 ms |
3284 KB |
Output is correct |
42 |
Correct |
3 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3028 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
2 ms |
3156 KB |
Output is correct |
46 |
Correct |
2 ms |
3072 KB |
Output is correct |
47 |
Correct |
2 ms |
3540 KB |
Output is correct |
48 |
Correct |
2 ms |
2772 KB |
Output is correct |
49 |
Correct |
2 ms |
2816 KB |
Output is correct |
50 |
Correct |
2 ms |
2772 KB |
Output is correct |
51 |
Correct |
27 ms |
24196 KB |
Output is correct |
52 |
Correct |
215 ms |
181964 KB |
Output is correct |
53 |
Correct |
49 ms |
44108 KB |
Output is correct |
54 |
Correct |
29 ms |
25940 KB |
Output is correct |
55 |
Correct |
40 ms |
34984 KB |
Output is correct |
56 |
Correct |
24 ms |
21972 KB |
Output is correct |
57 |
Correct |
38 ms |
33176 KB |
Output is correct |
58 |
Correct |
6 ms |
6408 KB |
Output is correct |
59 |
Correct |
5 ms |
6028 KB |
Output is correct |
60 |
Correct |
2 ms |
3212 KB |
Output is correct |
61 |
Incorrect |
34 ms |
9048 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |