#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
const ld INF=2e9;
const ll MAXN=1e5+6;
const ll MAXK=202;
ll dp[MAXN][2],a[MAXN];
int last[MAXN][MAXK];
struct line
{
ld a,b;
ll pos;
ld llersect(line const& other)
{
if(a==other.a)return -INF;
return (ld)(other.b-b)/(a-other.a);
}
ld gety(ld x)
{
return a*x+b;
}
};
struct ConvexHull
{
deque<pair<line,double> >dq;
void add(line l)
{
while(!dq.empty()&&l.llersect(dq.back().first)<=dq.back().second)
{
dq.pop_back();
}
if(dq.empty())dq.push_back({l,-INF});
else dq.push_back({l,l.llersect(dq.back().first)});
}
pair<ld,int> query(ld x)
{
while(dq.size()>1&&dq[1].second<=x)dq.pop_front();
//cout<<dq.front().first.a<<" "<<dq.front().first.b<<endl;
return {dq.front().first.gety(x),dq.front().first.pos};
}
}ch[2];
ll n,k;
ll pref[MAXN];
int main()
{
cin>>n>>k;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
pref[i]=pref[i-1]+a[i];
}
/*for(ll i=0;i<=n;i++)
{
for(ll j=0;j<=k+1;j++)dp[i][j]=-INF;
}*/
//for(ll i=1;i<=k+1;i++)ch[i].add({0,-INF});
ch[0].add({0,0,0});
for(int j=1;j<=k+1;j++)
{
ch[j%2].dq.clear();
ch[j%2].add({0,-INF,-1});
for(int i=1;i<=n;i++)
{
tie(dp[i][j%2],last[i][j])=ch[1-j%2].query(pref[i]);
//cout<<i<<" "<<j<<" "<<last[i][j]<<endl;
}
for(int i=1;i<=n;i++)
{
ch[j%2].add({(ld)pref[i],(ld)dp[i][j%2]-pref[i]*pref[i],i});
}
}
cout<<dp[n][1-k%2]<<endl;
vector<ll>v;
ll n1=n;
for(ll i=k;i>=1;i--)
{
v.push_back(last[n1][i+1]);
//cout<<n1<<" "<<i+1<<endl;
n1=last[n1][i+1];
}
/*
for(ll i=k;i>=1;i--)
{
for(ll l1=l-1;l1>=0;l1--)
{
if(dp[l1][i]+(pref[l]-pref[l1])*pref[l1]==dp[l][i+1])
{
v.push_back(l1);
l=l1;
break;
}
}
}*/
for(auto xd:v)cout<<xd<<" ";
cout<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
364 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Incorrect |
1 ms |
384 KB |
Integer 2 violates the range [1, 1] |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
236 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
1 ms |
384 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
1 ms |
364 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
3 ms |
492 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
1 ms |
492 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
2 ms |
512 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Incorrect |
3 ms |
492 KB |
Integer 200 violates the range [1, 199] |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1260 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
1260 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
13 ms |
1260 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
1260 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
14 ms |
1260 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
13 ms |
1260 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
14 ms |
1260 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
12 ms |
1260 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
4 ms |
1260 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
6 ms |
1260 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9068 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
9 ms |
9452 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
122 ms |
9660 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
10 ms |
9580 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
85 ms |
9324 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
97 ms |
9196 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
107 ms |
9600 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
83 ms |
9600 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
92 ms |
9452 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
118 ms |
9452 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
90092 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
85 ms |
90732 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Correct |
1440 ms |
92908 KB |
contestant found the optimal answer: 497313449256899208 == 497313449256899208 |
4 |
Correct |
95 ms |
92908 KB |
contestant found the optimal answer: 374850090734572421 == 374850090734572421 |
5 |
Correct |
1469 ms |
90732 KB |
contestant found the optimal answer: 36183271951 == 36183271951 |
6 |
Correct |
1031 ms |
89580 KB |
contestant found the optimal answer: 51629847150471 == 51629847150471 |
7 |
Correct |
1136 ms |
92928 KB |
contestant found the optimal answer: 124074747024496432 == 124074747024496432 |
8 |
Correct |
1006 ms |
92524 KB |
contestant found the optimal answer: 309959349080800 == 309959349080800 |
9 |
Correct |
1060 ms |
91756 KB |
contestant found the optimal answer: 124113525649823701 == 124113525649823701 |
10 |
Correct |
1322 ms |
91776 KB |
contestant found the optimal answer: 124309619349406845 == 124309619349406845 |