이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define endl '\n'
#define ll long long
#define int long long
const long long inf=1e18;
const int MOD=998244353;
const int N=3e5+5;
int n,k,a[N];
pair<int,int> dp[N][2];
pair<int,int> mx(pair<int,int> p1,pair<int,int> p2){
if(p1.first==p2.first && p2.second<p1.second)swap(p1,p2);
if(p2.first>p1.first)return p2;
else return p1;
}
pair<int,int> func(int lambda){
dp[0][0]={0,0};//empty
dp[0][1]={-lambda,1};//using it
for(int i=1;i<=n;i++){
dp[i][0]=mx(dp[i-1][0],dp[i-1][1]);
int x=dp[i-1][0].first-lambda+a[i],y=dp[i-1][1].first+a[i];
if(x>y || (x==y && dp[i-1][0]<dp[i-1][1])){
dp[i][1].first=x;
dp[i][1].second=dp[i-1][0].second+1;
}
else{
dp[i][1].first=y;
dp[i][1].second=dp[i-1][1].second;
}
}
return max(dp[n][0],dp[n][1]);
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
int l=0,r=1e16;
while(l<r){
int mid=(l+r)/2;
int x=func(mid).second;
if(x>k)l=mid+1;
else r=mid;
}
auto p = func(l);
cout<<p.first+l*p.second;
}
# | 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... |