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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
const ll INF = 1e18;
void solve(){
int n, k; cin>>n>>k;
vi a(n+1); FOR(i,1,n+1) cin>>a[i];
vector<ll> s(n+1); FOR(i,1,n+1) s[i]=s[i-1]+a[i];
vector<ll> dp(n+1);
ll l=0,r=1e15;
while (r-l>1){
ll m=(l+r)/2;
vi seg(n+1); FOR(i,1,n+1) dp[i]=INF;
ll mx=0;
int cur=0;
FOR(i,1,n+1){
dp[i]=s[i]+mx-m;
seg[i]=cur+1;
if (dp[i-1]>dp[i]){
dp[i]=dp[i-1];
seg[i]=seg[i-1];
}
if (dp[i]-s[i]==mx) cur=max(cur,seg[i]);
if (dp[i]-s[i]>mx){
mx=dp[i]-s[i]; cur=seg[i];
}
}
if (seg[n]<k) r=m;
else l=m;
}
vi seg(n+1); FOR(i,1,n+1) dp[i]=INF;
ll mx=0;
int cur=0;
FOR(i,1,n+1){
dp[i]=s[i]+mx-l;
seg[i]=cur+1;
if (dp[i-1]>dp[i]){
dp[i]=dp[i-1];
seg[i]=seg[i-1];
}
if (dp[i]-s[i]==mx) cur=max(cur,seg[i]);
if (dp[i]-s[i]>mx){
mx=dp[i]-s[i]; cur=seg[i];
}
}
cout<<dp[n]+(ll)k*(ll)l;
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:67:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |