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>
using namespace std;
using ll = long long;
using ld = long double;
constexpr int N = int(3e5) + 5;
constexpr int MOD = int(1e9) + 7;
int a[N];
void solve(){
int n, k;
cin >> n >> k;
for(int i{}; i < n; ++i){
cin >> a[i];
}
ll l{}, r{ll(3e14)}, ans{};
while(l <= r){
ll m = l + r >> 1;
pair<ll, int> cur{a[0] - m, 1}, dp{max(cur, {})};
for(int i = 1; i < n; ++i){
cur = max(pair{cur.first + a[i], cur.second}, {dp.first + a[i] - m, dp.second + 1});
dp = max(dp, cur);
}
if(dp.second >= k){
ans = max(ans, dp.first + k * m);
l = m + 1;
}
else r = m - 1;
}
cout << ans;
}
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; ++i) {
solve();
}
}
Compilation message (stderr)
feast.cpp: In function 'void solve()':
feast.cpp:19:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | ll 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... |