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;
#define fi first
#define se second
#define int long long
#define sq(i) (int)(i)*(i)
const int N = 1e5 + 1;
const int K = 201;
const int oo = 1e18;
int n, k, best, cost;
int ps[N], dp[N][2], trace[N][K];
void compute(int j, int l, int r, int optl, int optr){
if (l > r) return;
int mid = (l + r) >> 1;
best = oo;
for (int i = optl; i <= min(mid, optr); ++i){
cost = sq(ps[mid] - ps[i - 1]);
if (best >= dp[i - 1][0] + cost){
best = dp[i - 1][0] + cost;
trace[mid][j] = i;
}
}
dp[mid][1] = best;
int opt = trace[mid][j];
compute(j, l, mid - 1, optl, opt);
compute(j, mid + 1, r, opt, optr);
}
void pray(){
cin >> n >> k;
for (int i = 1; i <= n; ++i){
cin >> ps[i];
ps[i] += ps[i - 1];
}
for (int i = 1; i <= n; ++i){
dp[i][0] = sq(ps[i]);
}
for (int j = 2; j <= k + 1; ++j){
compute(j, 1, n, 1, n);
for (int i = 1; i <= n; ++i){
dp[i][0] = dp[i][1];
}
}
int ans = (sq(ps[n]) - dp[n][1]) >> 1ll;
cout << ans << "\n";
vector<int> path;
int y = k + 1, x = trace[n][y];
for (; y > 1; --y, x = trace[x - 1][y]){
path.push_back(x - 1);
}
reverse(path.begin(), path.end());
for (int i : path) cout << i << " ";
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tests = 1; // cin >> tests;
while (tests--) pray();
}
# | 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... |