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 pb push_back
#define F first
#define pii pair<int,int>
#define all(a) a.begin(),a.end()
#define S second
#define sz(a) (int)a.size()
#define rep(i , a , b) for(int i = (a) ; i <= (b) ; i++)
#define per(i , a , b) for(int i = (a) ; i >= (b) ; i--)
#define int long long
using namespace std ;
const int maxn = 3e5 + 10 , mod = 1e9 + 7 , inf = 1e18 ;
pii dp[maxn][2] ;
int a[maxn] , n , k ;
void mx(pii &a , pii b){
if(a.F < b.F || (a.F==b.F && a.S < b.S))a = b;
}
pii ch(int x){
dp[0][0] = {0 ,0};
dp[0][1] = {-inf , 0} ;
rep(i ,1, n){
dp[i][0] = dp[i-1][0] ;
mx(dp[i][0] , dp[i-1][1]) ;
dp[i][1] = {dp[i-1][1].F+a[i] , dp[i-1][1].S};
mx(dp[i][1] , {dp[i-1][0].F+a[i]-x , dp[i-1][0].S+1}) ;
}
pii ans = dp[n][0] ;
mx(ans , dp[n][1]);
return ans ;
}
signed main(){
ios_base::sync_with_stdio(false) ; cin.tie(0) ;
cin >> n >> k;
rep(i ,1, n){
cin >> a[i] ;
}
int l = 0 , r= 1e18 ;
while(r-l>1) {
int mid = (l+r)/2 ;
if(ch(mid).S >= k){
l = mid ;
}else{
r = mid ;
}
}
cout << ch(l).F + l*k << "\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... |