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 ll;
const int N = 1e5 + 7;
const int K = 207;
const ll INF = 8e18;
int n, k, a[N], s[N];
ll dp[2][N];
int trace[K][N];
void Calc(int k, int l, int r, int from, int to){
if (l > r)
return;
int mid = (l + r) >> 1;
for(int i = from; i <= min(mid - 1, to); i++){
ll cost = (ll) (s[mid] - s[i]) * (s[n] - s[mid]);
if (dp[1][mid] < dp[0][i] + cost){
dp[1][mid] = dp[0][i] + cost;
trace[k][mid] = i;
}
}
Calc(k, l, mid - 1, from, trace[k][mid]);
Calc(k, mid + 1, r, trace[k][mid], to);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
for(int i = 0; i < 2; i++)
for(int j = 0; j <= n; j++)
dp[i][j] = -INF;
dp[1][0] = 0;
for(int i = 1; i <= k + 1; i++){
for(int j = 0; j <= n; j++){
dp[0][j] = dp[1][j];
dp[1][j] = -INF;
}
Calc(i, 1, n, 0, n - 1);
}
cout << dp[1][n] << '\n';
int cur = n;
for(int i = k + 1; i > 1; i--){
cur = trace[i][cur];
cout << cur << ' ';
}
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... |