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 ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
int mod = 1000000007;
int prv[N][200];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<ll> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
vector<ll> dp(n + 1);
for (int i = 1; i <= k; i++) {
vector<ll> ndp(n + 1);
function<void(int, int, int, int)> Solve = [&] (int l, int r, int opl, int opr) {
if (l > r) return;
ll val = -1e18;
int best = 0;
int mid = l + r >> 1;
for (int j = opl; j <= min(opr, mid); j++) {
ll x = dp[j - 1] + a[j - 1] * (a[mid] - a[j - 1]);
if (x >= val) {
val = x;
best = j;
}
}
prv[mid][i] = best;
ndp[mid] = val;
Solve(l, mid - 1, opl, best);
Solve(mid + 1, r, best, opr);
};
Solve(1, n, 1, n);
swap(dp, ndp);
}
cout << dp[n] << en;
vector<int> ans;
int j = n;
for (int i = k; i >= 1; i--) {
ans.push_back(prv[j][i] - 1);
j = prv[j][i] - 1;
}
while (!ans.empty()) {
cout << ans.back() << sp;
ans.pop_back();
}
cout << en;
return 0;
}
Compilation message (stderr)
sequence.cpp: In lambda function:
sequence.cpp:30:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = l + r >> 1;
| ~~^~~
# | 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... |