This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define pb push_back
#define pu push
#define ins insert
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ff(x,a,b,c) for (auto x=a;x<=b;x+=c)
#define fd(x,a,b,c) for (auto x=a;x>=b;x-=c)
#define int ll
using namespace std;
const int inf = 1e18;
const int N = 1e5+10;
int n,m;
int dp_before[N], dp_cur[N];
int pref[N];
int btrk[N][210];
int s[N];
long long C(int i, int j)
{
if (i>j) swap(i,j);
int ans = (pref[j]-pref[i-1])*pref[i-1];
return ans;
}
void compute(int l, int r, int optl, int optr,int i) {
if (l > r) return;
int mid = (l + r)/2;
pair<int, int> best = {-inf, -1};
for (int k = optl; k <= min(mid, optr); k++) {
best = max(best, {dp_before[k-1] + C(k,mid), k});
}
//btrk[mid][i] = best.second-1;
dp_cur[mid] = best.first;
int opt = best.second;
compute(l, mid - 1, optl, opt,i);
compute(mid + 1, r, opt, optr,i);
}
long long solve1() {
for (int i = 1; i <= n; i++) dp_before[i] = C(1,i);
for (int i = 2; i <= m+1; i++) {
compute(1, n , 1, n, i);
for(int i=0;i<=n;i++) dp_before[i] = dp_cur[i];
}
return dp_before[n];
}
void solve()
{
cin>>n>>m;
int sum = 0;
for (int i=1;i<=n;i++) cin>>s[i],sum+=s[i];
for (int i=1;i<=n;i++) pref[i] = pref[i-1]+s[i];;
int ans = solve1();
cout<<ans<<endl;
int i=n,j=m+1;
while(j>0){
if(btrk[i][j]!=0) cout<<btrk[i][j]<<" ";
i = btrk[i][j]; j--;
}
}
signed main()
{
bruh
int t = 1;
//cin>>t;
while (t--)
{
solve();
cout<<endl;
}
}
# | 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... |