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>
// #define int long long
#define F first
#define S second
#define kill(x) cout << x << endl, exit(0);
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)1e5 + 7;
const int K = 207;
const int MOD = 998244353;
const ll INF = (ll)1e18 + 7;
int n, m, t, k, q, u, v, w, tmp, tmp2, tmp3, tmp4, tmp5, flag, ans;
int arr[MAXN], opt[MAXN][K];
ll ps[MAXN], dp[MAXN][K];
ll cost(ll x, ll k, ll s) {
ll res = ps[x]-ps[s-1];
return dp[s-1][k-1]+res*(res-1)/2;
}
#define mid ((l+r)>>1)
void Divide(int ind, int l, int r, int s, int t) {
if (l > r) return;
for (int i=s; i<=min(mid, t); i++) {
if (opt[mid][ind] == -1 || cost(mid, ind, opt[mid][ind]) > cost(mid, ind, i)) opt[mid][ind] = i;
}
dp[mid][ind] = cost(mid, ind, opt[mid][ind]);
Divide(ind, l, mid-1, s, opt[mid][ind]);
Divide(ind, mid+1, r, opt[mid][ind], t);
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
k++;
for (int i=1; i<=n; i++) {
cin >> arr[i];
ps[i] = ps[i-1]+arr[i];
}
for (int j=0; j<=k; j++) for (int i=0; i<=n; i++) dp[i][j] = INF, opt[i][j] = -1;
dp[0][0] = 0;
for (int i=1; i<=k; i++) Divide(i, 1, n, 1, n);
cout << ps[n]*(ps[n]-1)/2 - dp[n][k] << endl;
tmp = n;
for (int i=0; i<k-1; i++) cout << (tmp = opt[tmp][k-i]-1) << " ";
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... |