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;
const int maxn = 1e5 + 5;
const long long inf = 1e16;
int n, a[maxn], k;
long long ps[maxn];
long long pref[maxn];
long long f[maxn], g[maxn];
int trace[maxn][205];
long long cost(int l, int r) {
return (1ll * (ps[r] - ps[l - 1]) * (ps[r] - ps[l - 1]) - (pref[r] - pref[l - 1])) / 2;
}
void calc(int k, int l, int r, int optl, int optr) {
if(l > r) return;
long long res = inf;
int pos = -1;
int mid = (l + r) / 2;
for(int i = optl; i <= min(mid, optr); ++i) {
if(g[i - 1] + cost(i, mid) < res) {
res = g[i - 1] + cost(i, mid);
pos = i;
}
}
f[mid] = res;
trace[mid][k] = pos;
calc(k, l, mid - 1, optl, pos);
calc(k, mid + 1, r, pos, optr);
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i] * a[i];
ps[i] = ps[i - 1] + a[i];
}
for(int i = 1; i <= n; ++i) {
g[i] = cost(1, i);
}
for(int i = 2; i <= k + 1; ++i) {
calc(i, 1, n, 1, n);
for(int j = 1; j <= n; ++j) {
g[j] = f[j];
f[j] = 0;
}
}
cout << cost(1, n) - g[n] << '\n';
vector<int> vec;
++k;
while(k > 1) {
vec.push_back(trace[n][k] - 1);
n = trace[n][k] - 1;
--k;
}
reverse(vec.begin(), vec.end());
for(auto i:vec) cout << i << " ";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
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... |