#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5;
int n,k,idx=1,a[N+5],pre[N+5],bef1[N+5][205];
vector <int> bef(N+5),dp(N+5);
int Cal(int i, int j){
return (pre[j]-pre[i-1])*(pre[n]-pre[j]);
}
void dnc(int l, int r, int optl, int optr){
if (l>r) return;
int md=l+r>>1,ans=-1e18,pos=0;
for (int i=min(optr,md);i>=optl;--i){
int d=bef[i-1];//+Cal(i,md);
if (d>ans){
ans=d;
pos=i;
}
}
dp[md]=ans;
bef1[md][idx]=pos;
dnc(l,md-1,optl,pos);
dnc(md+1,r,pos,optr);
}
void Solve(){
cin>>n>>k;
for (int i=1;i<=n;++i){
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
for (int i=1;i<=n;++i) bef[i]=Cal(1,i);
for (int i=2;i<=k;++i){
++idx;
dnc(i,n,i,n);
for (int j=1;j<=n;++j){
bef[j]=dp[j];
dp[j]=0;
}
}
int ans=-1e18,pos;
for (int i=1;i<=n;++i)
if (bef[i]>ans){
ans=bef[i];
pos=i;
}
cout<<ans<<"\n";
for (int i=k;i>=1;--i){
cout<<pos<<' ';
pos=bef1[pos][i]-1;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen ("FILE.INP","r",stdin);
// freopen ("FILE.OUT","w",stdout);
int t=1;
// cin>>t;
while (t--) Solve();
}
Compilation message
sequence.cpp: In function 'void dnc(long long int, long long int, long long int, long long int)':
sequence.cpp:12:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int md=l+r>>1,ans=-1e18,pos=0;
| ~^~
sequence.cpp: In function 'void Solve()':
sequence.cpp:49:24: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
49 | pos=bef1[pos][i]-1;
| ~~~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1880 KB |
declared answer doesn't correspond to the split scheme: declared = 72, real = 92 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1884 KB |
Integer 50 violates the range [1, 49] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4188 KB |
Integer 200 violates the range [1, 199] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4108 KB |
Integer 1000 violates the range [1, 999] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
18012 KB |
declared answer doesn't correspond to the split scheme: declared = 1196550000, real = 1496550000 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
53 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |