#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[maxn], cnt[maxn];
void trydp(int lam)
{
memset(dp, 0, sizeof dp);
memset(cnt, 0, sizeof cnt);
for (int i = 1, cur = 0; i <= n; i++) {
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
if (maxi(dp[i], cur + S[i] - lam)) cnt[i] = cnt[i - 1] + 1;
maxi(cur, dp[i] - S[i]);
}
}
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[n] > k) {
l = mid + 1;
}
else {
r = mid;
}
}
trydp(r);
cout << dp[n] + cnt[n] * 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:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
10832 KB |
Output is correct |
2 |
Correct |
54 ms |
10844 KB |
Output is correct |
3 |
Correct |
54 ms |
10884 KB |
Output is correct |
4 |
Incorrect |
55 ms |
10840 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
12876 KB |
Output is correct |
2 |
Correct |
63 ms |
12632 KB |
Output is correct |
3 |
Correct |
62 ms |
12764 KB |
Output is correct |
4 |
Correct |
63 ms |
12768 KB |
Output is correct |
5 |
Correct |
62 ms |
12636 KB |
Output is correct |
6 |
Correct |
64 ms |
12636 KB |
Output is correct |
7 |
Correct |
64 ms |
12628 KB |
Output is correct |
8 |
Correct |
63 ms |
12628 KB |
Output is correct |
9 |
Correct |
64 ms |
12884 KB |
Output is correct |
10 |
Correct |
66 ms |
12872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7000 KB |
Output is correct |
2 |
Correct |
6 ms |
7260 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Incorrect |
5 ms |
7000 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7000 KB |
Output is correct |
2 |
Correct |
6 ms |
7260 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Incorrect |
5 ms |
7000 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7000 KB |
Output is correct |
2 |
Correct |
6 ms |
7260 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Incorrect |
5 ms |
7000 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |