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;
using ld = long double;
#define int long long
#define pb push_back
#define F first
#define S second
int dp[2][205][100010];
struct line{
int slope, c;
int eval(int x){
return slope*x+c;
}
int interx(line & a){
return (a.c-c)/(slope-a.slope);
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int a[n];
int pref[n+1];
pref[0]=0;
for(int i=0; i< n; i++){
cin >> a[i];
pref[i+1]=pref[i]+a[i];
}
for(int j=1; j<=k; j++){
deque<pair<line, int>> cht;
cht.pb({{-pref[j-1], dp[0][j-1][j]}, j});
for(int i=j+1; i<=n; i++){
int x = pref[n]-pref[i-1];
while(cht.size()>=2){
pair<line, int> l = cht.back();
cht.pop_back();
if(l.F.eval(x)>cht.back().F.eval(x)){
cht.push_back(l);
break;
}
}
dp[0][j][i]=cht.back().F.eval(x)+pref[i-1]*(pref[n]-pref[i-1]);
dp[1][j][i]=cht.back().S;
pair<line, int> np = {{-pref[i-1], dp[0][j-1][i]}, i};
while(cht.size()>=2){
pair<line, int> f = cht.front();
cht.pop_front();
if(f.F.slope==np.F.slope){
if(f.F.c<=np.F.c)continue;
}
int inter1 = f.F.interx(cht.front().F);
int inter2 = np.F.interx(cht.front().F);
if(inter2<inter1){
cht.push_front(f);
break;
}
}
cht.push_front(np);
}
}
/*
cout << '\n';
for(int j=1; j<=k; j++){
for(int i=1; i<=n; i++){
cout << dp[1][j][i] << " ";
}
cout << '\n';
}
cout << '\n';
for(int j=1; j<=k; j++){
for(int i=1; i<=n; i++){
cout << dp[0][j][i] << " ";
}
cout << '\n';
}
cout << '\n';*/
int ans=0;
int ind=0;
for(int i=1; i<=n; i++){
if(dp[0][k][i]>=ans){
ans=dp[0][k][i];
ind=i;
}
}
ind--;
cout << ans << '\n';
vector<int> con;
con.pb(ind);
for(int i=k-1; i>0; i--){
con.pb(dp[1][i][ind]);
ind=dp[1][i][ind];
}
sort(con.begin(), con.end());
for(int i=0; i<k; i++){
cout << con[i] << " ";
}
int sum=pref[n]*pref[n];
sum-=pref[con[0]]*pref[con[0]]+(pref[n]-pref[con[k-1]])*(pref[n]-pref[con[k-1]]);
for(int i=1; i<k; i++)sum-=(pref[con[i]]-pref[con[i-1]])*(pref[con[i]]-pref[con[i-1]]);
assert(sum/2==ans);
cout << '\n';
}
# | 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... |