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 N = 1e5+5;
int n,m;
vector<int> dp_before, dp_cur;
int pref[N];
int btrk[N][210];
long long C(int i, int j)
{
if (i>j) swap(i,j);
return (pref[j]-pref[i-1])*(pref[j]-pref[i-1]);
}
void compute(int l, int r, int optl, int optr,int i) {
if (l > r) return;
int mid = (l + r)/2;
pair<long long, int> best = {LLONG_MAX, -1};
for (int k = optl; k <= min(mid, optr); k++) {
best = min(best, {(k ? dp_before[k - 1] : 0) + 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() {
dp_before.resize(n+1,0);
dp_cur.resize(n+1,0);
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);
dp_before = dp_cur;
}
return dp_before[n];
}
void solve()
{
cin>>n>>m;
vector<int> s(n+1);
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 = (sum*sum-solve1())/2;
cout<<ans<<endl;
int i=n,j=m+1;
while(j>0){
if(btrk[i][j]) 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... |