#include <bits/stdc++.h>
using namespace std;
#define name "splitsequence"
#define MAXN 100005
#define pb push_back
#define pf push_front
#define ll long long
#define ii pair<int, int>
#define fs first
#define sc second
#define ill pair<int, ll>
#define lli pair<ll, int>
#define llll pair<ll, ll>
#define all(v) v.begin(),v.end()
#define uni(v) v.erase(unique(all(v)),v.end())
#define bit(n,i) (((n)>>(i))&1)
#define FOR(i,a,b) for (int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for (int i=(a),_b=(b); i>=_b; i--)
#define MASK(i) (1LL<<(i))
const int INF=1e9;
const int MOD=1e9+7;
void add(int &u, int v){
u+=v;
if (u>=MOD) u-=MOD;
}
void sub(int &u, int v){
u-=v;
if (u<0) u+=MOD;
}
void minimize(int &u, int v){
u=min(u,v);
}
bool maximize(ll &u, ll v){
if (u<=v){
u=v;
return 1;
}
return 0;
}
long long Rand(long long l, long long r){
ll tmp=0;
FOR(i,1,4) tmp=((tmp<<15)^(((1<<15)-1)&rand()));
return l+tmp%(r-l+1);
}
int n,k;
int a[MAXN];
ll dp[MAXN][2];
int trace[MAXN][202];
ll sum[MAXN];
ll get(int l, int r){
if (l>r) return 0;
return sum[r]-sum[l-1];
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>k;
FOR(i,1,n) cin>>a[i];
FOR(i,1,n) sum[i]=sum[i-1]+1LL*a[i];
memset(dp,-0x3f,sizeof(dp));
dp[0][0]=0;
ll res=0;
int u=-1;
FOR(g,1,k){
int j=g-1;
FOR(i,g,n-1){
/*
FOR(j,g-1,i-1){
if (maximize(dp[i][g&1],dp[j][(g&1)^1]+1LL*get(j+1,i)*get(i+1,n))){
trace[i][g]=j;
}
}
*/
while (j<i-1&&dp[j][(g&1)^1]+get(j+1,i)*get(i+1,n)<=dp[j+1][(g&1)^1]+get(j+2,i)*get(i+1,n)){
++j;
}
dp[i][g&1]=dp[j][(g&1)^1]+get(j+1,i)*get(i+1,n);
trace[i][g]=j;
if (maximize(res,dp[i][g&1])){
u=i;
}
}
}
cout<<res<<"\n";
int v=k;
vector<int> vec;
while (u>0){
vec.pb(u);
u=trace[u][v];
--v;
}
reverse(all(vec));
for (int x:vec) cout<<x<<" ";
return 0;
}
# | 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... |