This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
const int64_t INF = 1ll << 60;
const int N = 3e5 + 5;
int n, k, a[N], ans;
bool check(const int O, bool f = 0) {
ar dp{0, 0}, F{0, 0};
int s = 0;
FOR(i, 0, n) {
s += a[i];
F = max(F, ar{dp[0] - s, dp[1]});
dp = max(dp, ar{s + F[0] - O, F[1] - 1});
}
if (f) ans = max(ans, dp[0] + k * O);
return -dp[1] >= k;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
FOR(i, 0, n) cin >> a[i];
int l = 0, r = 1ll << 50;
while (r - l > 1) {
int m = l + r >> 1;
if (check(m)) l = m;
else r = m;
}
check(l, 1);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:31:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int m = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |