Submission #409523

#TimeUsernameProblemLanguageResultExecution timeMemory
409523penguinhackerFeast (NOI19_feast)C++14
100 / 100
157 ms8064 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array const int mxN=3e5; int n, k, a[mxN], cnt[mxN]; ll dp[mxN]; int solve(ll x) { ll s=0; ar<ll, 2> cur={0, 0}; for (int i=0; i<n; ++i) { s+=a[i]; dp[i]=i?dp[i-1]:0; cnt[i]=i?cnt[i-1]:0; if (cur[0]+s-x>dp[i]) dp[i]=cur[0]+s-x, cnt[i]=cur[1]+1; if (dp[i]-s>cur[0]) cur={dp[i]-s, cnt[i]}; } return cnt[n-1]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i=0; i<n; ++i) cin >> a[i]; ll lb=0, rb=1e15; while(lb<rb) { ll m=(lb+rb)/2; if (solve(m)<=k) rb=m; else lb=m+1; } solve(lb); cout << dp[n-1]+k*lb; return 0; }
#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...