# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1094225 | Trisanu_Das | Feast (NOI19_feast) | C++17 | 0 ms | 0 KiB |
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;
#define int long long
#define ff first
#define ss second
int n, k, a[300005];
pair<int, int> dp[300005];
pair<int, int> calc(int mid){
pair<int, int> curr(0,0);
for(int i = 1; i <= n; i++){
dp[i] = max(dp[i - 1], {curr.ff + a[i] - mid, curr.ss + 1});
curr = max(curr, {dp[i].ff - a[i], dp[i].ss});
}
return dp[n];
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) a[i] += a[i - 1];
int l = 0, r = 3e14;
while(l < r){
int mid = (l + r + 1) / 2;
if(calc(mid).ss >= k) l = mid;
else r = m - 1;
}
cout << calc(l).ff + l * k << '\n';
}