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;
#define int long long
using db = long double;
const int maxn = 1E5+5;
int n, K, l = 1, r = 1, a[maxn], p[maxn], Q[maxn], bk[maxn];
vector<int> dp, new_dp;
bool m(int j, int k, int i) {
return ((dp[k] - dp[j]) * (p[i] - p[k])) <= ((dp[i] - dp[k]) * (p[k] - p[j]));
}
bool idk(int j, int k, int i) {
return (p[n] - p[i]) * (p[k] - p[j]) <= (dp[k] - dp[j]);
}
int32_t main() {
cin >> n >> K;
new_dp.assign(n+1, 0);
dp.assign(n+1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[i] = p[i-1] + a[i];
}
for (int i = 1; i <= n; i++) {
dp[i] = p[i] * (p[n] - p[i]);
}
for (int kk = 2; kk <= K; kk++) {
for (int i = 1; i <= n; i++) Q[i] = 0;
for (int i = n; i >= 1; i--) new_dp[i] = dp[i];
l = r = 1;
for (int i = 1; i < kk; i++) Q[r++] = i;
--r;
for (int i = kk; i <= n; i++) {
while (r - l >= 1 && idk(Q[l], Q[l+1], i)) l++;
if (l <= r) {
new_dp[i] = dp[Q[l]] + (p[i] - p[Q[l]]) * (p[n] - p[i]);
bk[i] = Q[l];
}
while (r - l >= 1 && m(Q[r-1], Q[r], i)) r--;
r++; Q[r] = i;
}
swap(new_dp, dp);
}
int stupid = n;
vector<int> pth;
K++;
while (K--) {
pth.push_back(stupid);
stupid = bk[stupid];
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
reverse(pth.begin(), pth.end());
pth.pop_back();
for (auto x : pth) cout << x << ' ';
cout << '\n';
}
# | 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... |