#include<cstdio>
#include<algorithm>
#include<deque>
#define MOD (ll)(1e7)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pp;
ll d[2][1010],s[100010],N,K;
short r[201][1001];
void f(int x,int y){
if(x==1)return;
f(x-1,r[x][y]);
printf("%d ",(int)r[x][y]);
}
struct convex{
deque<ll> a,b,c;
void init(){a.clear();b.clear();c.clear();}
/*bool check(ll z0,ll z1,ll z2,ll z3){
ll uz0=z0/MOD,uz1=z1/MOD,uz2=z2/MOD,uz3=z3/MOD;
z0%=MOD,z1%=MOD,z2%=MOD,z3%=MOD;
ll p=0,q=0,r=0,p2=0,q2=0,r2=0;
r=z0*z1,r2=z2*z3;
q+=r/MOD,q2+=r2/MOD;
r%=MOD,r2%=MOD;
q+=z0*uz1+uz0*z1,q2+=z2*uz3+z3*uz2;
p+=q/MOD,p2+=q2/MOD;
q%=MOD,q2%=MOD;
p+=uz0*uz1,p2+=uz2*uz3;
if(p!=p2)return p>p2;
if(q!=q2)return q>q2;
return r>r2;
}*/
void push(ll n,ll m,ll v){
while(a.size()>1&&(b[b.size()-2]-b.back())*(n-a.back())>(b.back()-m)*(a.back()-a[a.size()-2])){
a.pop_back();
b.pop_back();
c.pop_back();
}
a.push_back(n);
b.push_back(m);
c.push_back(v);
}
pp query(ll x){
while(!a.empty()&&(a[1]-a[0])*x>(b[0]-b[1])){
a.pop_front();
b.pop_front();
c.pop_front();
}
return pp(a[0]*x+b[0],c[0]);
}
};
int main(){
scanf("%lld%lld",&N,&K);
if(K==0)K++;
for(int i=1;i<=N;i++){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}
convex* pre=new convex;
convex* cur=new convex;
pp ret;
for(int i=1;i<=N;i++){
d[1][i]=s[i]*(s[N]-s[i]);
pre->push(s[i],d[1][i]-s[i]*s[N],i);
}
if(K==1){
ll Max=-1,v=0;
for(int i=1;i<=N;i++){
if(d[1][i]>Max){
Max=d[1][i];
v=i;
}
}
printf("%lld\n%lld",Max,v);
return 0;
}
for(int g=2;g<=K+1;g++){
for(int i=g;i<=N;i++){
ret=pre->query(s[i]);
d[g&1][i]=ret.first+(s[N]-s[i])*s[i];
r[g][i]=ret.second;
cur->push(s[i],d[g&1][i]-s[i]*s[N],i);
}
pre->init();
swap(cur,pre);
}
printf("%lld\n",d[(K+1)&1][N]);
f(K+1,N);
delete cur,pre;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 100400096 == 100400096 |
10 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 900320000 == 900320000 |
11 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 3698080248 == 3698080248 |
12 |
Correct |
0 ms |
2772 KB |
Output is correct - contestant found the optimal answer: 3200320000 == 3200320000 |
13 |
Incorrect |
0 ms |
2772 KB |
Output isn't correct - contestant didn't find the optimal answer: 140067 < 140072 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |