#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{
long long dp[2005][2005][2];
void solve(void) {
FOR(i, 0, N) FOR(j, 0, K) dp[i][j][0] = dp[i][j][1] = -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 (N <= 2000) sub1.solve();
sub2.solve();
}
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
file("FEAST");
//int T; cin >> T; while (T--)
solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
/*
6 2
1 2 3 -10 5 6
*/
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:9:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:70:5: note: in expansion of macro 'file'
70 | file("FEAST");
| ^~~~
feast.cpp:9:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:70:5: note: in expansion of macro 'file'
70 | file("FEAST");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6192 KB |
Output is correct |
2 |
Correct |
30 ms |
6164 KB |
Output is correct |
3 |
Correct |
30 ms |
6088 KB |
Output is correct |
4 |
Correct |
33 ms |
6184 KB |
Output is correct |
5 |
Correct |
41 ms |
6092 KB |
Output is correct |
6 |
Correct |
31 ms |
6020 KB |
Output is correct |
7 |
Correct |
29 ms |
6060 KB |
Output is correct |
8 |
Correct |
30 ms |
6088 KB |
Output is correct |
9 |
Correct |
32 ms |
6052 KB |
Output is correct |
10 |
Correct |
32 ms |
6120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
6028 KB |
Output is correct |
2 |
Correct |
22 ms |
6120 KB |
Output is correct |
3 |
Correct |
25 ms |
6068 KB |
Output is correct |
4 |
Correct |
21 ms |
7256 KB |
Output is correct |
5 |
Correct |
32 ms |
8896 KB |
Output is correct |
6 |
Correct |
21 ms |
7116 KB |
Output is correct |
7 |
Correct |
23 ms |
7304 KB |
Output is correct |
8 |
Correct |
34 ms |
9024 KB |
Output is correct |
9 |
Correct |
32 ms |
8796 KB |
Output is correct |
10 |
Correct |
24 ms |
7196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
6128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2900 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2940 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
1 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2900 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2940 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
1 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
4052 KB |
Output is correct |
12 |
Correct |
2 ms |
3924 KB |
Output is correct |
13 |
Correct |
2 ms |
3796 KB |
Output is correct |
14 |
Correct |
2 ms |
3924 KB |
Output is correct |
15 |
Correct |
2 ms |
3924 KB |
Output is correct |
16 |
Correct |
2 ms |
3796 KB |
Output is correct |
17 |
Correct |
2 ms |
4052 KB |
Output is correct |
18 |
Correct |
2 ms |
3796 KB |
Output is correct |
19 |
Correct |
2 ms |
3796 KB |
Output is correct |
20 |
Correct |
2 ms |
3924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2900 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2940 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
1 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
4052 KB |
Output is correct |
12 |
Correct |
2 ms |
3924 KB |
Output is correct |
13 |
Correct |
2 ms |
3796 KB |
Output is correct |
14 |
Correct |
2 ms |
3924 KB |
Output is correct |
15 |
Correct |
2 ms |
3924 KB |
Output is correct |
16 |
Correct |
2 ms |
3796 KB |
Output is correct |
17 |
Correct |
2 ms |
4052 KB |
Output is correct |
18 |
Correct |
2 ms |
3796 KB |
Output is correct |
19 |
Correct |
2 ms |
3796 KB |
Output is correct |
20 |
Correct |
2 ms |
3924 KB |
Output is correct |
21 |
Correct |
13 ms |
16724 KB |
Output is correct |
22 |
Correct |
42 ms |
61540 KB |
Output is correct |
23 |
Correct |
14 ms |
22356 KB |
Output is correct |
24 |
Correct |
11 ms |
17108 KB |
Output is correct |
25 |
Correct |
11 ms |
19924 KB |
Output is correct |
26 |
Correct |
10 ms |
16128 KB |
Output is correct |
27 |
Correct |
9 ms |
19412 KB |
Output is correct |
28 |
Correct |
5 ms |
11732 KB |
Output is correct |
29 |
Correct |
5 ms |
11564 KB |
Output is correct |
30 |
Correct |
4 ms |
10580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6192 KB |
Output is correct |
2 |
Correct |
30 ms |
6164 KB |
Output is correct |
3 |
Correct |
30 ms |
6088 KB |
Output is correct |
4 |
Correct |
33 ms |
6184 KB |
Output is correct |
5 |
Correct |
41 ms |
6092 KB |
Output is correct |
6 |
Correct |
31 ms |
6020 KB |
Output is correct |
7 |
Correct |
29 ms |
6060 KB |
Output is correct |
8 |
Correct |
30 ms |
6088 KB |
Output is correct |
9 |
Correct |
32 ms |
6052 KB |
Output is correct |
10 |
Correct |
32 ms |
6120 KB |
Output is correct |
11 |
Correct |
21 ms |
6028 KB |
Output is correct |
12 |
Correct |
22 ms |
6120 KB |
Output is correct |
13 |
Correct |
25 ms |
6068 KB |
Output is correct |
14 |
Correct |
21 ms |
7256 KB |
Output is correct |
15 |
Correct |
32 ms |
8896 KB |
Output is correct |
16 |
Correct |
21 ms |
7116 KB |
Output is correct |
17 |
Correct |
23 ms |
7304 KB |
Output is correct |
18 |
Correct |
34 ms |
9024 KB |
Output is correct |
19 |
Correct |
32 ms |
8796 KB |
Output is correct |
20 |
Correct |
24 ms |
7196 KB |
Output is correct |
21 |
Incorrect |
35 ms |
6128 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |