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 fi first
#define se second
#define lc id<<1
#define rc id<<1^1
#define nmax 100008
const long long inf = 1e18;
using namespace std;
typedef pair<int, int> pii;
int n, k, row, a[nmax], f[nmax];
long long sum[nmax], dp[208][nmax], trace[208][nmax];
void dq(int l, int r, int u, int v){
if (l > r)
return;
int mid = (l + r) >> 1, sp = -1;
dp[row][mid] = inf;
for (int i = u; i <= mid && i <= v; i++)
if (dp[row][mid] > dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1]){
dp[row][mid] = dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1];
sp = i;
}
trace[row][mid] = sp;
dq(l, mid - 1, u, sp);
dq(mid + 1, r, sp, v);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
f[i] = f[i - 1] + a[i];
sum[i] = sum[i - 1] + a[i] * f[i - 1];
dp[0][i] = inf;
}
k++;
dp[0][0] = 0;
for (row = 1; row <= k; row++)
dq(row, n, row, n);
cout << sum[n] - dp[k][n] << '\n';
for (int j = k, i = n; j > 0; j--){
i = trace[j][i] - 1;
if (i)
cout << i << ' ';
}
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... |