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;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update>;
#define int long long
#define endl '\n'
#define mod 1000000007
//\
#define mod 1686876991
const int maxn = 100001;
int n, k;
int a[maxn];
int p[maxn];
int dp[201][maxn];
int opts[201][maxn];
int cost (int l, int r) {
// cerr << "cost(" << l << "," << r << "): " << (p[r] - (l ? p[l - 1] : 0)) * (p[n-1] - p[r]) << endl;
return (p[r] - (l ? p[l - 1] : 0)) * (p[n-1] - p[r]);
}
void rec (int l, int r, int lk, int rk, int j) {
if (r < l) return;
int m = (l + r) / 2;
int opt;
dp[j][m] = -INT_MAX;
for (int K = j - 1; K <= min(rk, m) ; K++) {
if ((K ? dp[j - 1][K-1] : 0) + cost(K, m) > dp[j][m]) {
opt = K;
dp[j][m] = (K ? dp[j - 1][K - 1] : 0) + cost(K, m);
}
}
opts[j][m] = opt;
rec(l, m - 1, lk, opt, j);
rec(m + 1, r, opt, rk, j);
}
signed main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 0 ; i < n ; i++) {
cin >> a[i];
p[i] = a[i];
if (i) p[i] += p[i-1];
}
for (int i = 0 ; i < n ; i++) {
dp[1][i] = cost(0, i);
}
for (int i = 2 ; i <= k + 1 ; i++) {
rec(0, n - 1, 0, n - 1, i);
}
cout << dp[k + 1][n-1] << endl;
int i = k + 1;
int j = n - 1;
vector<int> v;
while (i > 1) {
v.push_back(opts[i][j]);
j = opts[i][j] - 1;
i--;
}
reverse(v.begin(), v.end());
for (int i : v) cout << i << " ";
}
Compilation message (stderr)
sequence.cpp:14:1: warning: multi-line comment [-Wcomment]
14 | //\
| ^
sequence.cpp: In function 'void rec(long long int, long long int, long long int, long long int, long long int)':
sequence.cpp:43:8: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | rec(l, m - 1, lk, opt, j);
| ~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |