Submission #778852

#TimeUsernameProblemLanguageResultExecution timeMemory
778852dooweyFeast (NOI19_feast)C++14
100 / 100
130 ms10412 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)3e5 + 10;
const ll inf = (ll)1e18;
ll P[N];

pii dp[N];
int n;

void compute(ll penalty){
    for(int i = 1 ; i <= n; i ++ ){
        dp[i]=mp(-inf,0);
    }
    pii cur=mp(dp[0].fi-P[0]-penalty, 1);
    for(int i = 1; i <= n; i ++ ){
        dp[i]=max(dp[i-1],mp(cur.fi+P[i],cur.se));
        cur=max(cur,mp(dp[i].fi-P[i]-penalty,dp[i].se+1));
    }
}

int main(){
    fastIO;
    int k;
    cin >> n >> k;
    for(int i = 1; i <= n; i ++ ){
        cin >> P[i];
        P[i]+=P[i-1];
    }
    ll lf = 0ll;
    ll rf = (ll)1e18;
    ll mid;
    ll penalty;

    while(lf < rf){
        mid = (lf + rf) / 2;
        compute(mid);
        if(dp[n].se > k){
            lf=mid+1;
        }
        else{
            rf=mid;
        }
    }
    compute(lf);
    cout << dp[n].fi + dp[n].se * 1ll * lf << "\n";
    return 0;
}

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:42:8: warning: unused variable 'penalty' [-Wunused-variable]
   42 |     ll penalty;
      |        ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...