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 N 100005
#define K 205
#define ll long long
typedef pair<ll,int> pa;
struct line{
ll m,c;
int idx;
line(ll m,ll c,int i) : m(m), c(c), idx(i) {}
ll dot(ll x){ return m*x + c;}
ll inter(line l){ return floor((long double)(c-l.c)/(l.m-m));}
};
struct CHT{
deque<line> hull;
void add(line l){
if(hull.size() >= 1 && hull.back().m == l.m && hull.back().c == l.c) hull.pop_back();
while(hull.size() >= 2 && l.inter(hull.back()) <= hull.back().inter(hull[hull.size()-2])) hull.pop_back();
hull.push_back(l);
}
pa query(ll x){
while(hull.size() >= 2 && hull[0].dot(x) <= hull[1].dot(x)) hull.pop_front();
return {hull[0].dot(x), hull[0].idx};
}
};
int n,k,a[N],pre[N],P[K][N],pos=1;
ll dp[2][N],ans=0;
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]), pre[i] = pre[i-1] + a[i];
for(int i=1;i<n;i++){
dp[1][i] = 1ll*(pre[n] - pre[i]) * pre[i];
if(1 == k && dp[1][i] > ans){
ans = dp[1][i];
pos = i;
}
}
for(int i=2;i<=k;i++){
int now = i%2, prv = now^1;
CHT cht;
for(int j=i;j<n;j++){
cht.add(line(pre[j-1], 1ll * -pre[j-1] * pre[n] + dp[prv][j-1], j-1));
pa mx = cht.query(pre[j]);
long long res = mx.first, idx = mx.second;
dp[now][j] = res + 1ll * pre[j] * pre[n] - 1ll * pre[j] * pre[j];
P[i][j] = idx;
if(i == k && dp[now][j] > ans){
ans = dp[now][j];
pos = j;
}
}
}
stack<int> stk;
while(k){
stk.push(pos);
pos = P[k--][pos];
}
printf("%lld\n",ans);
while(!stk.empty()){
printf("%d ",stk.top());
stk.pop();
}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
sequence.cpp:32:29: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | for(int i=1;i<=n;i++) scanf("%d",&a[i]), pre[i] = pre[i-1] + a[i];
| ~~~~~^~~~~~~~~~~~
# | 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... |