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 ll long long
#define ii pair<int, int>
#define fi first
#define se second
#define pll pair<ll, ll>
#define db long double
#define plll pair<ll, pll>
const int maxn = 4e5+5;
const ll mod = 1e9+7;
const int iinf = 1e9+7;
const ll inf = 1e18+7;
const double eps = 1e-9;
const db pi = acos(-1);
int n, k;
int a[maxn];
void inp(){
cin >> n >> k;
for(int i=1; i<=n; i++){
cin >> a[i];
}
}
pll d[maxn][2];
pll check(ll lmb){
d[1][0] = {0, 0};
d[1][1] = {a[1] - lmb, 1};
for(int i=2; i<=n; i++){
d[i][0] = max(d[i-1][0], d[i-1][1]);
d[i][1] = max(make_pair(d[i-1][0].fi + a[i] - lmb, d[i-1][0].se + 1),
make_pair(d[i-1][1].fi + a[i], d[i-1][1].se));
}
return max({d[n][0], d[n][1]});
}
void solve(){
ll l = 0, r = inf;
ll res = 0;
while(l <= r){
ll mid = (l+r)/2;
if(check(mid).se >= k){
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << check(res).fi + res * k << "\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--){
inp();
solve();
}
cerr << "\nTime elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s.\n";
}
# | 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... |