#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>
inline bool maxi(T &x, const T &val)
{
if (x < val) return x = val, true;
return false;
}
template<typename T>
inline bool mini(T &x, const T &val)
{
if (x > val) return x = val, true;
return false;
}
const int maxn = 3e5 + 10, oo = 1e15;
int n, k;
int a[maxn], S[maxn];
int dp, cnt;
void trydp(int lam)
{
dp = cnt = 0;
for (int i = 1, cur = 0, curcnt = 0; i <= n; i++) {
if (maxi(dp, cur + S[i] - lam)) cnt = curcnt + 1;
if (maxi(cur, dp - S[i])) curcnt = cnt;
}
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i], S[i] = S[i - 1] + a[i];
int l = 0, r = oo;
while (l < r) {
int mid = (l + r) >> 1;
trydp(mid);
if (cnt > k) {
l = mid + 1;
}
else {
r = mid;
}
}
trydp(r);
cout << dp + cnt * r << '\n';
}
signed main()
{
#ifdef LOCAL
clock_t st = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
#define Task ""
#ifdef LOCAL
if (!fopen("D:\\.inp", "r")) {
freopen("D:\\.inp", "w", stdout);
freopen("D:\\.out", "w", stdout);
cerr << "get input from file\n";
return 0;
}
freopen("D:\\.inp", "r", stdin);
freopen("D:\\.out", "w", stdout);
#else
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
#endif
solve();
#ifdef LOCAL
cerr << clock() - st << endl;
#endif
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4956 KB |
Output is correct |
2 |
Correct |
36 ms |
5012 KB |
Output is correct |
3 |
Correct |
37 ms |
4956 KB |
Output is correct |
4 |
Correct |
36 ms |
5132 KB |
Output is correct |
5 |
Correct |
46 ms |
5004 KB |
Output is correct |
6 |
Correct |
40 ms |
5204 KB |
Output is correct |
7 |
Correct |
35 ms |
4952 KB |
Output is correct |
8 |
Correct |
38 ms |
4956 KB |
Output is correct |
9 |
Correct |
35 ms |
4952 KB |
Output is correct |
10 |
Correct |
35 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4956 KB |
Output is correct |
2 |
Correct |
28 ms |
4956 KB |
Output is correct |
3 |
Correct |
32 ms |
5004 KB |
Output is correct |
4 |
Correct |
28 ms |
4952 KB |
Output is correct |
5 |
Correct |
35 ms |
4956 KB |
Output is correct |
6 |
Correct |
28 ms |
4956 KB |
Output is correct |
7 |
Correct |
29 ms |
5212 KB |
Output is correct |
8 |
Correct |
36 ms |
4948 KB |
Output is correct |
9 |
Correct |
36 ms |
4948 KB |
Output is correct |
10 |
Correct |
29 ms |
5004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
4948 KB |
Output is correct |
2 |
Correct |
56 ms |
4956 KB |
Output is correct |
3 |
Correct |
58 ms |
4944 KB |
Output is correct |
4 |
Correct |
55 ms |
5008 KB |
Output is correct |
5 |
Correct |
56 ms |
5096 KB |
Output is correct |
6 |
Correct |
59 ms |
5000 KB |
Output is correct |
7 |
Correct |
60 ms |
5128 KB |
Output is correct |
8 |
Correct |
54 ms |
4952 KB |
Output is correct |
9 |
Correct |
55 ms |
5004 KB |
Output is correct |
10 |
Correct |
56 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4956 KB |
Output is correct |
2 |
Correct |
36 ms |
5012 KB |
Output is correct |
3 |
Correct |
37 ms |
4956 KB |
Output is correct |
4 |
Correct |
36 ms |
5132 KB |
Output is correct |
5 |
Correct |
46 ms |
5004 KB |
Output is correct |
6 |
Correct |
40 ms |
5204 KB |
Output is correct |
7 |
Correct |
35 ms |
4952 KB |
Output is correct |
8 |
Correct |
38 ms |
4956 KB |
Output is correct |
9 |
Correct |
35 ms |
4952 KB |
Output is correct |
10 |
Correct |
35 ms |
4956 KB |
Output is correct |
11 |
Correct |
28 ms |
4956 KB |
Output is correct |
12 |
Correct |
28 ms |
4956 KB |
Output is correct |
13 |
Correct |
32 ms |
5004 KB |
Output is correct |
14 |
Correct |
28 ms |
4952 KB |
Output is correct |
15 |
Correct |
35 ms |
4956 KB |
Output is correct |
16 |
Correct |
28 ms |
4956 KB |
Output is correct |
17 |
Correct |
29 ms |
5212 KB |
Output is correct |
18 |
Correct |
36 ms |
4948 KB |
Output is correct |
19 |
Correct |
36 ms |
4948 KB |
Output is correct |
20 |
Correct |
29 ms |
5004 KB |
Output is correct |
21 |
Correct |
56 ms |
4948 KB |
Output is correct |
22 |
Correct |
56 ms |
4956 KB |
Output is correct |
23 |
Correct |
58 ms |
4944 KB |
Output is correct |
24 |
Correct |
55 ms |
5008 KB |
Output is correct |
25 |
Correct |
56 ms |
5096 KB |
Output is correct |
26 |
Correct |
59 ms |
5000 KB |
Output is correct |
27 |
Correct |
60 ms |
5128 KB |
Output is correct |
28 |
Correct |
54 ms |
4952 KB |
Output is correct |
29 |
Correct |
55 ms |
5004 KB |
Output is correct |
30 |
Correct |
56 ms |
4952 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
0 ms |
2396 KB |
Output is correct |
43 |
Correct |
0 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2392 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2392 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
2 ms |
2396 KB |
Output is correct |
61 |
Correct |
86 ms |
4956 KB |
Output is correct |
62 |
Correct |
85 ms |
4944 KB |
Output is correct |
63 |
Correct |
67 ms |
4948 KB |
Output is correct |
64 |
Correct |
88 ms |
5120 KB |
Output is correct |
65 |
Correct |
91 ms |
5004 KB |
Output is correct |
66 |
Correct |
89 ms |
5108 KB |
Output is correct |
67 |
Correct |
91 ms |
5068 KB |
Output is correct |
68 |
Correct |
58 ms |
5000 KB |
Output is correct |
69 |
Correct |
56 ms |
5144 KB |
Output is correct |
70 |
Correct |
55 ms |
5000 KB |
Output is correct |