#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
using pli = pair<i64, int>;
const int MAXN = 3e5 + 5;
const int oo32 = 1e9 + 5;
const i64 oo64 = 1e18 + 5;
int N, K;
i64 a[MAXN];
namespace subtask1 {
bool is_subtask() {
for(int i = 1; i <= N; i++) {
if(a[i] < 0) return false;
}
return true;
}
void solution() {
if(is_subtask() == false) return;
i64 ans = accumulate(a + 1, a + N + 1, 0LL);
cout << ans;
exit(0);
}
}
namespace subtask2 {
bool is_subtask() {
int c = 0;
for(int i = 1; i <= N; i++)
c += (a[i] < 0);
return c <= 1;
}
void solution() {
if(is_subtask() == false) return;
i64 tot = accumulate(a + 1, a + N + 1, 0LL);
i64 ans = tot;
for(int i = 1; i <= N; i++) ans = max(ans, tot - a[i]);
cout << ans;
exit(0);
}
}
namespace subtask3 {
bool is_subtask() {
return K == 1;
}
i64 dp[MAXN];
void solution() {
if(is_subtask() == false) return;
for(int i = 1; i <= N; i++) dp[i] = max(dp[i - 1], 0LL) + a[i];
cout << *max_element(dp + 1, dp + N + 1);
exit(0);
}
}
namespace subtask {
void solution() {
for(int i = 1; i <= N; i++) a[i] += a[i - 1];
vector<vector<i64>> dp(N + 1, vector<i64>(K + 1, -oo64));
vector<i64> maxx(N + 1, 0);
dp[0][0] = 0;
for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], -a[i]);
for(int j = 1; j <= K; j++) {
for(int i = 1; i <= N; i++)
maximize(dp[i][j], maxx[i - 1] + a[i]);
for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], dp[i][j]);
for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], maxx[i] - a[i]);
}
i64 ans = 0;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= K; j++)
maximize(ans, dp[i][j]);
}
cout << ans;
}
}
namespace solver {
pli dp[MAXN][2];
void calc(i64 mid) {
dp[1][0] = {0, 0}, dp[1][1] = {a[1] - mid, 1};
for(int i = 2; i <= N; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][0].first + a[i] - mid, dp[i - 1][0].second + 1),
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second));
}
}
void solution() {
i64 low = 0, high = oo64;
i64 ans = 0;
while(low <= high) {
i64 mid = (low + high) / 2;
calc(mid);
if(max(dp[N][0], dp[N][1]).second >= K) {
ans = mid;
low = mid + 1;
} else high = mid - 1;
}
calc(ans);
cout << max(dp[N][0], dp[N][1]).first + K * ans;
}
}
signed main() {
#define task "code"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> K;
for(int i = 1; i <= N; i++) cin >> a[i];
subtask1::solution();
subtask3::solution();
subtask2::solution();
solver::solution();
return (0 ^ 0);
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4096 KB |
Output is correct |
2 |
Correct |
22 ms |
4056 KB |
Output is correct |
3 |
Correct |
22 ms |
4344 KB |
Output is correct |
4 |
Correct |
23 ms |
3920 KB |
Output is correct |
5 |
Correct |
22 ms |
3920 KB |
Output is correct |
6 |
Correct |
22 ms |
4176 KB |
Output is correct |
7 |
Correct |
24 ms |
3920 KB |
Output is correct |
8 |
Correct |
23 ms |
4680 KB |
Output is correct |
9 |
Correct |
22 ms |
4232 KB |
Output is correct |
10 |
Correct |
23 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4944 KB |
Output is correct |
2 |
Correct |
16 ms |
5060 KB |
Output is correct |
3 |
Correct |
17 ms |
4944 KB |
Output is correct |
4 |
Correct |
16 ms |
3920 KB |
Output is correct |
5 |
Correct |
29 ms |
4980 KB |
Output is correct |
6 |
Correct |
17 ms |
4944 KB |
Output is correct |
7 |
Correct |
15 ms |
3920 KB |
Output is correct |
8 |
Correct |
24 ms |
4944 KB |
Output is correct |
9 |
Correct |
24 ms |
4936 KB |
Output is correct |
10 |
Correct |
16 ms |
3920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
5120 KB |
Output is correct |
2 |
Correct |
25 ms |
5080 KB |
Output is correct |
3 |
Correct |
28 ms |
4944 KB |
Output is correct |
4 |
Correct |
33 ms |
4952 KB |
Output is correct |
5 |
Correct |
28 ms |
5032 KB |
Output is correct |
6 |
Correct |
27 ms |
6984 KB |
Output is correct |
7 |
Correct |
26 ms |
5192 KB |
Output is correct |
8 |
Correct |
29 ms |
4936 KB |
Output is correct |
9 |
Correct |
28 ms |
5192 KB |
Output is correct |
10 |
Correct |
26 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2556 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
2 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2600 KB |
Output is correct |
25 |
Correct |
1 ms |
2556 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
2 ms |
2384 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
4096 KB |
Output is correct |
2 |
Correct |
22 ms |
4056 KB |
Output is correct |
3 |
Correct |
22 ms |
4344 KB |
Output is correct |
4 |
Correct |
23 ms |
3920 KB |
Output is correct |
5 |
Correct |
22 ms |
3920 KB |
Output is correct |
6 |
Correct |
22 ms |
4176 KB |
Output is correct |
7 |
Correct |
24 ms |
3920 KB |
Output is correct |
8 |
Correct |
23 ms |
4680 KB |
Output is correct |
9 |
Correct |
22 ms |
4232 KB |
Output is correct |
10 |
Correct |
23 ms |
5204 KB |
Output is correct |
11 |
Correct |
15 ms |
4944 KB |
Output is correct |
12 |
Correct |
16 ms |
5060 KB |
Output is correct |
13 |
Correct |
17 ms |
4944 KB |
Output is correct |
14 |
Correct |
16 ms |
3920 KB |
Output is correct |
15 |
Correct |
29 ms |
4980 KB |
Output is correct |
16 |
Correct |
17 ms |
4944 KB |
Output is correct |
17 |
Correct |
15 ms |
3920 KB |
Output is correct |
18 |
Correct |
24 ms |
4944 KB |
Output is correct |
19 |
Correct |
24 ms |
4936 KB |
Output is correct |
20 |
Correct |
16 ms |
3920 KB |
Output is correct |
21 |
Correct |
27 ms |
5120 KB |
Output is correct |
22 |
Correct |
25 ms |
5080 KB |
Output is correct |
23 |
Correct |
28 ms |
4944 KB |
Output is correct |
24 |
Correct |
33 ms |
4952 KB |
Output is correct |
25 |
Correct |
28 ms |
5032 KB |
Output is correct |
26 |
Correct |
27 ms |
6984 KB |
Output is correct |
27 |
Correct |
26 ms |
5192 KB |
Output is correct |
28 |
Correct |
29 ms |
4936 KB |
Output is correct |
29 |
Correct |
28 ms |
5192 KB |
Output is correct |
30 |
Correct |
26 ms |
5200 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2508 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2556 KB |
Output is correct |
36 |
Correct |
1 ms |
2384 KB |
Output is correct |
37 |
Correct |
1 ms |
2384 KB |
Output is correct |
38 |
Correct |
1 ms |
2384 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
2384 KB |
Output is correct |
41 |
Correct |
1 ms |
2384 KB |
Output is correct |
42 |
Correct |
1 ms |
2384 KB |
Output is correct |
43 |
Correct |
1 ms |
2384 KB |
Output is correct |
44 |
Correct |
1 ms |
2384 KB |
Output is correct |
45 |
Correct |
1 ms |
2384 KB |
Output is correct |
46 |
Correct |
1 ms |
2384 KB |
Output is correct |
47 |
Correct |
1 ms |
2520 KB |
Output is correct |
48 |
Correct |
1 ms |
2384 KB |
Output is correct |
49 |
Correct |
1 ms |
2384 KB |
Output is correct |
50 |
Correct |
1 ms |
2384 KB |
Output is correct |
51 |
Correct |
2 ms |
2384 KB |
Output is correct |
52 |
Correct |
1 ms |
2384 KB |
Output is correct |
53 |
Correct |
1 ms |
2384 KB |
Output is correct |
54 |
Correct |
1 ms |
2600 KB |
Output is correct |
55 |
Correct |
1 ms |
2556 KB |
Output is correct |
56 |
Correct |
1 ms |
2384 KB |
Output is correct |
57 |
Correct |
1 ms |
2384 KB |
Output is correct |
58 |
Correct |
1 ms |
2384 KB |
Output is correct |
59 |
Correct |
2 ms |
2384 KB |
Output is correct |
60 |
Correct |
1 ms |
2384 KB |
Output is correct |
61 |
Correct |
109 ms |
17244 KB |
Output is correct |
62 |
Correct |
110 ms |
17480 KB |
Output is correct |
63 |
Correct |
93 ms |
17340 KB |
Output is correct |
64 |
Correct |
112 ms |
17668 KB |
Output is correct |
65 |
Correct |
116 ms |
17480 KB |
Output is correct |
66 |
Correct |
118 ms |
17536 KB |
Output is correct |
67 |
Correct |
122 ms |
17224 KB |
Output is correct |
68 |
Correct |
83 ms |
17480 KB |
Output is correct |
69 |
Correct |
82 ms |
17244 KB |
Output is correct |
70 |
Correct |
83 ms |
17228 KB |
Output is correct |