#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 |
39 ms |
4952 KB |
Output is correct |
2 |
Correct |
50 ms |
7764 KB |
Output is correct |
3 |
Correct |
38 ms |
7804 KB |
Output is correct |
4 |
Correct |
37 ms |
7760 KB |
Output is correct |
5 |
Correct |
39 ms |
7924 KB |
Output is correct |
6 |
Correct |
36 ms |
7764 KB |
Output is correct |
7 |
Correct |
36 ms |
7760 KB |
Output is correct |
8 |
Correct |
37 ms |
7924 KB |
Output is correct |
9 |
Correct |
37 ms |
7772 KB |
Output is correct |
10 |
Correct |
37 ms |
7764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4952 KB |
Output is correct |
2 |
Correct |
29 ms |
4956 KB |
Output is correct |
3 |
Correct |
31 ms |
5248 KB |
Output is correct |
4 |
Correct |
28 ms |
4956 KB |
Output is correct |
5 |
Correct |
36 ms |
7760 KB |
Output is correct |
6 |
Correct |
28 ms |
5980 KB |
Output is correct |
7 |
Correct |
29 ms |
6232 KB |
Output is correct |
8 |
Correct |
38 ms |
7928 KB |
Output is correct |
9 |
Correct |
36 ms |
7768 KB |
Output is correct |
10 |
Correct |
29 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
4956 KB |
Output is correct |
2 |
Correct |
56 ms |
4956 KB |
Output is correct |
3 |
Correct |
56 ms |
5016 KB |
Output is correct |
4 |
Correct |
58 ms |
4952 KB |
Output is correct |
5 |
Correct |
56 ms |
4956 KB |
Output is correct |
6 |
Correct |
57 ms |
4956 KB |
Output is correct |
7 |
Correct |
56 ms |
5124 KB |
Output is correct |
8 |
Correct |
54 ms |
4944 KB |
Output is correct |
9 |
Correct |
56 ms |
4956 KB |
Output is correct |
10 |
Correct |
56 ms |
5124 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 |
2396 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 |
2396 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 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 |
2392 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 |
2396 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 |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 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 |
2392 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 |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2528 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2540 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 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 |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4952 KB |
Output is correct |
2 |
Correct |
50 ms |
7764 KB |
Output is correct |
3 |
Correct |
38 ms |
7804 KB |
Output is correct |
4 |
Correct |
37 ms |
7760 KB |
Output is correct |
5 |
Correct |
39 ms |
7924 KB |
Output is correct |
6 |
Correct |
36 ms |
7764 KB |
Output is correct |
7 |
Correct |
36 ms |
7760 KB |
Output is correct |
8 |
Correct |
37 ms |
7924 KB |
Output is correct |
9 |
Correct |
37 ms |
7772 KB |
Output is correct |
10 |
Correct |
37 ms |
7764 KB |
Output is correct |
11 |
Correct |
28 ms |
4952 KB |
Output is correct |
12 |
Correct |
29 ms |
4956 KB |
Output is correct |
13 |
Correct |
31 ms |
5248 KB |
Output is correct |
14 |
Correct |
28 ms |
4956 KB |
Output is correct |
15 |
Correct |
36 ms |
7760 KB |
Output is correct |
16 |
Correct |
28 ms |
5980 KB |
Output is correct |
17 |
Correct |
29 ms |
6232 KB |
Output is correct |
18 |
Correct |
38 ms |
7928 KB |
Output is correct |
19 |
Correct |
36 ms |
7768 KB |
Output is correct |
20 |
Correct |
29 ms |
6236 KB |
Output is correct |
21 |
Correct |
57 ms |
4956 KB |
Output is correct |
22 |
Correct |
56 ms |
4956 KB |
Output is correct |
23 |
Correct |
56 ms |
5016 KB |
Output is correct |
24 |
Correct |
58 ms |
4952 KB |
Output is correct |
25 |
Correct |
56 ms |
4956 KB |
Output is correct |
26 |
Correct |
57 ms |
4956 KB |
Output is correct |
27 |
Correct |
56 ms |
5124 KB |
Output is correct |
28 |
Correct |
54 ms |
4944 KB |
Output is correct |
29 |
Correct |
56 ms |
4956 KB |
Output is correct |
30 |
Correct |
56 ms |
5124 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 |
2396 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 |
2392 KB |
Output is correct |
42 |
Correct |
1 ms |
2392 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2392 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 |
2392 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 |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2528 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2540 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 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 |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
90 ms |
7924 KB |
Output is correct |
62 |
Correct |
87 ms |
8196 KB |
Output is correct |
63 |
Correct |
69 ms |
7896 KB |
Output is correct |
64 |
Correct |
89 ms |
8016 KB |
Output is correct |
65 |
Correct |
92 ms |
8152 KB |
Output is correct |
66 |
Correct |
91 ms |
8016 KB |
Output is correct |
67 |
Correct |
90 ms |
8052 KB |
Output is correct |
68 |
Correct |
59 ms |
8160 KB |
Output is correct |
69 |
Correct |
59 ms |
8180 KB |
Output is correct |
70 |
Correct |
60 ms |
7912 KB |
Output is correct |