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>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = 1e5+7;
ll dp[mxn][207], a[mxn], sum = 0, all_pair = 0, ans = 0, pfs[mxn], pfs_2[mxn];
int n, k, trace[mxn][207];
ll get(ll l, ll r)
{
ll ans = (pfs[r]-pfs[l-1])*(pfs[r]-pfs[l-1])-pfs_2[r]+pfs_2[l-1];
return ans>>1;
}
vector<vector<ll>> opt;
void dnc(ll j, ll l, ll r, ll opt_l, ll opt_r)
{
ll m = (r+l)>>1;
pair<ll,ll> res = {1e18, opt_l};
for (ll i = min(m,opt_r); i >= opt_l; i--)
{
ll c = dp[i-1][j-1]+get(i,m);
if (res.fi > c) {res = {c,i};}
}
dp[m][j] = res.fi; trace[m][j] = res.se;
if (l <= m-1) dnc(j,l,m-1,opt_l,res.se);
if (m+1 <= r) dnc(j,m+1,r,res.se,opt_r);
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
cin >> n >> k; k++;
for (ll i = 1; i <= n; i++)
{
cin >> a[i];
pfs[i] = pfs[i-1]+a[i];
pfs_2[i] = pfs_2[i-1]+a[i]*a[i];
}
for (ll i = 1; i <= n; i++) {dp[i][1] = get(1,i);}
for (ll i = 2; i <= k; i++) dnc(i,1,n,1,n);
cout << get(1,n)-dp[n][k] << '\n';
vector<ll> v;
while (k > 1)
{
v.pb(trace[n][k]-1);
n = trace[n][k]-1;
k--;
}
reverse(v.begin(),v.end());
for (ll i : v) cout << i << ' ';
}
# | 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... |