#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;
struct line{
ll m, n, id;
ll getval(ll x){
return m*x+n;
}
ll xinter(const line &tar){
return (tar.n-n) / (m-tar.m);
}
};
void solve(){
ll n, k;
cin>>n>>k;
vll tep(n);
for(ll i=0;i<n;i++){
cin>>tep[i];
}
vll a;
vll idx;
for(ll i=0;i<n;i++){
if(tep[i]!=0){
a.pb(tep[i]);
idx.pb(i);
}
}
ll siz=a.size();
if(siz<=k+1){
ll ans=0;
ll pre=0;
for(ll i=0;i<siz;i++){
ans+=a[i]*pre;
pre+=a[i];
}
cout<<ans<<'\n';
ll cnt=0;
for(auto it:idx){
if(it>=n-1) break;
cout<<it+1<<' ';
cnt++;
}
for(ll i=0;i<n && cnt<k;i++){
if(tep[i]==0){
cout<<i+1<<' ';
cnt++;
}
}
cout<<'\n';
return;
}
vector<vll> dp(n+1, vll(k+1));
vector<vll> par(n+1, vll(k+1));
vll pre(siz+1);
for(ll i=0;i<siz;i++){
pre[i+1]=pre[i]+a[i];
}
//for()
for(ll tar=2;tar<=k;tar++){
deque<line> dq;
dq.pb({0, 0, 0});
for(ll i=1;i<=siz;i++){
while(dq.size()>=2 && dq[0].getval(pre[i])<dq[1].getval(pre[i])){
dq.pop_front();
}
dp[i][tar]=dq[0].getval(pre[i]);
par[i][tar]=dq[0].id;
line nline={pre[i], dp[i][tar-1]-pre[i]*pre[i], i};
while(dq.size()>=2 && dq[(int) dq.size()-1].xinter(nline)<
dq[(int) dq.size()-2].xinter(nline)){
dq.pop_back();
}
dq.pb(nline);
}
}
for(ll i=1;i<=siz;i++){
dp[i][k]=dp[i][k]+(pre[siz]-pre[i])*pre[i];
}
ll ans=0;
ll cur=1;
for(ll i=1;i<=siz-1;i++){
if(dp[i][k]>ans){
ans=dp[i][k];
cur=i;
}
}
cout<<ans<<'\n';
vll ans1;
for(ll i=k;i>=1;i--){
//assert(cur-1<-100);
if(cur==0){
cout<<"wrong\n";
break;
}
ans1.pb(idx[cur-1]+1);
//cout<<cur<<' ';
cur=par[cur][i];
}
reverse(ans1.begin(), ans1.end());
for(auto it:ans1){
cout<<it<<' ';
}
cout<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
/*
dp[k][i]=max j<i dp[k-1][j]+(pre[i]-pre[j])*pre[j]
=pre[j]*pre[i]+(dp[k-1][j]-pre[j]*pre[j])
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 108 == 108 |
2 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1 == 1 |
7 |
Incorrect |
0 ms |
344 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1093956 == 1093956 |
2 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 302460000 == 302460000 |
3 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 122453454361 == 122453454361 |
4 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 93663683509 == 93663683509 |
5 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 1005304678 == 1005304678 |
6 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 933702 == 933702 |
7 |
Correct |
1 ms |
344 KB |
contestant found the optimal answer: 25082842857 == 25082842857 |
8 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 687136 == 687136 |
9 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 27295930079 == 27295930079 |
10 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 29000419931 == 29000419931 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 610590000 == 610590000 |
2 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 311760000 == 311760000 |
3 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 1989216017013 == 1989216017013 |
4 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 1499437552673 == 1499437552673 |
5 |
Correct |
1 ms |
860 KB |
contestant found the optimal answer: 1019625819 == 1019625819 |
6 |
Correct |
0 ms |
348 KB |
contestant found the optimal answer: 107630884 == 107630884 |
7 |
Correct |
0 ms |
344 KB |
contestant found the optimal answer: 475357671774 == 475357671774 |
8 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 193556962 == 193556962 |
9 |
Correct |
1 ms |
348 KB |
contestant found the optimal answer: 482389919803 == 482389919803 |
10 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 490686959791 == 490686959791 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
contestant found the optimal answer: 21503404 == 21503404 |
2 |
Correct |
1 ms |
600 KB |
contestant found the optimal answer: 140412195 == 140412195 |
3 |
Correct |
6 ms |
3676 KB |
contestant found the optimal answer: 49729674225461 == 49729674225461 |
4 |
Correct |
1 ms |
600 KB |
contestant found the optimal answer: 37485571387523 == 37485571387523 |
5 |
Correct |
6 ms |
3676 KB |
contestant found the optimal answer: 679388326 == 679388326 |
6 |
Correct |
5 ms |
3164 KB |
contestant found the optimal answer: 4699030287 == 4699030287 |
7 |
Correct |
7 ms |
3676 KB |
contestant found the optimal answer: 12418819758185 == 12418819758185 |
8 |
Correct |
6 ms |
3472 KB |
contestant found the optimal answer: 31093317350 == 31093317350 |
9 |
Correct |
2 ms |
1116 KB |
contestant found the optimal answer: 12194625429236 == 12194625429236 |
10 |
Correct |
3 ms |
1628 KB |
contestant found the optimal answer: 12345131038664 == 12345131038664 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2136 KB |
contestant found the optimal answer: 1818678304 == 1818678304 |
2 |
Correct |
3 ms |
2140 KB |
contestant found the optimal answer: 1326260195 == 1326260195 |
3 |
Correct |
84 ms |
32860 KB |
contestant found the optimal answer: 4973126687469639 == 4973126687469639 |
4 |
Correct |
3 ms |
2136 KB |
contestant found the optimal answer: 3748491676694116 == 3748491676694116 |
5 |
Correct |
40 ms |
20436 KB |
contestant found the optimal answer: 1085432199 == 1085432199 |
6 |
Correct |
50 ms |
23388 KB |
contestant found the optimal answer: 514790755404 == 514790755404 |
7 |
Correct |
65 ms |
25180 KB |
contestant found the optimal answer: 1256105310476641 == 1256105310476641 |
8 |
Correct |
54 ms |
20832 KB |
contestant found the optimal answer: 3099592898816 == 3099592898816 |
9 |
Correct |
55 ms |
23616 KB |
contestant found the optimal answer: 1241131419367412 == 1241131419367412 |
10 |
Correct |
61 ms |
29784 KB |
contestant found the optimal answer: 1243084101967798 == 1243084101967798 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
18116 KB |
contestant found the optimal answer: 19795776960 == 19795776960 |
2 |
Correct |
21 ms |
18356 KB |
contestant found the optimal answer: 19874432173 == 19874432173 |
3 |
Runtime error |
64 ms |
131072 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |