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], old_dp[mxn], sum = 0, all_pair = 0, ans = 0, pfs_2[mxn];
int n, k, trace[mxn][207], a[mxn], pfs[mxn];
ll get(int l, int r)
{
ll ansx = ((ll)pfs[r]-pfs[l-1])*((ll)pfs[r]-pfs[l-1])-pfs_2[r]+pfs_2[l-1];
return ansx>>1;
}
void dnc(int j, int l, int r, int opt_l, int opt_r)
{
int m = (r+l)>>1;
pair<ll,int> res = {1e18, opt_l};
for (int i = min(m,opt_r); i >= opt_l; i--)
{
ll c = old_dp[i-1]+get(i,m);
if (res.fi > c) {res = {c,i};}
}
dp[m] = 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 (int 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 (int i = 1; i <= n; i++) {dp[i] = get(1,i);}
for (int i = 2; i <= k; i++)
{
for (ll j = 1; j <= n; j++) old_dp[j] = dp[j];
dnc(i,1,n,1,n);
}
cout << get(1,n)-dp[n] << '\n';
vector<int> v;
while (k > 1)
{
v.pb(trace[n][k]-1);
n = trace[n][k]-1;
k--;
}
reverse(v.begin(),v.end());
for (int 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... |