제출 #1358936

#제출 시각아이디문제언어결과실행 시간메모리
1358936NewtonabcFeast (NOI19_feast)C++20
18 / 100
108 ms12156 KiB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+10;
pair<ll,int> dp[N][2];
ll a[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,k; cin>>n >>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    ll l=1,r=3e14;
    pair<ll,int> ret={0,67};
    while(l<r){
        ll mid=(l+r+1LL)/2LL;
        dp[0][0]={0,0};
        dp[0][1]={-mid,-1};
        for(int i=1;i<=n;i++){
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
            pair<ll,int> cd=dp[i-1][0];
            cd.first+=a[i]-mid;
            cd.second--;
            dp[i][1]=cd;
            cd=dp[i-1][1];
            cd.first+=a[i];
            dp[i][1]=max(dp[i][1],cd);
        }
        ll ans=max(dp[n][0],dp[n][1]).second;
        if(-ans>=k) l=mid,ret=max(dp[n][0],dp[n][1]);
        else r=mid-1LL;
    }
    auto [val,opt]=ret;
    if(opt==67){
        cout<<0;
        return 0;
    }
    opt=-opt;
    //cout<<val <<" " <<opt <<"\n";
    val=val+l*k-(opt-k)*l;
    cout<<val;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…