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>
using namespace std;
#define int long long
#define ll long long
#define pii pair<ll,ll>
#define fi first
#define se second
const int MAXN= 3e5+5;
int N, K, A[MAXN], op;
pii dp[MAXN];
ll pref[MAXN];
// A : ambil K gratis
// B : ambil berapapun bayar
ll f(ll cost) {
pii m;
for (int i=1;i<=N;i++) {
dp[i] = max(dp[i-1], {m.fi+pref[i]-cost,m.se-1});
m = max(m,{dp[i].fi-pref[i],dp[i].se});
}
op = -dp[N].se;
return dp[N].fi;
}
signed main() {
cin>>N>>K;
for (int i=1;i<=N;i++) {
cin>>A[i];
pref[i] = pref[i-1] + A[i];
}
ll L=0, R=1e15, mid, res;
while (L<=R) {
mid=(L+R)/2;
f(mid);
if (op <= K) {
res = mid;
R = mid-1;
}
else {
L = mid+1;
}
}
cout<<f(res)+res*K<<endl;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:46:21: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
46 | cout<<f(res)+res*K<<endl;
| ~~~^~
# | 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... |