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
const int inf=1e18;
signed main() {
cin.tie(0)->sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<int> pre(n+1, 0);
for (int i = 1; i <= n; ++i) {
cin >> pre[i];
pre[i]+=pre[i-1];
}
vector<vector<int>> dp(n+1, vector<int>(2, 0)), prev(n+1, vector<int>(k+1, 0));
for (int j = 1; j <= k; ++j) {
deque<pair<pair<int, int>, pair<int, int>>> s;
s.push_back({{inf, 0}, {0, 0}});
for (int i = 1; i <= n; ++i) {
dp[i][j%2]=pre[i]*(pre[n]-pre[i]);
while (pre[i]>s.front().first.first) s.pop_front();
auto [m, c]=s.front().second;
dp[i][j%2]+=m*pre[i]+c;
prev[i][j]=s.front().first.second;
int cn=dp[i][(j-1)%2]-pre[n]*pre[i], mn=pre[i];
while (!s.empty()) {
auto [m, c]=s.back().second;
if (m==mn) {
if (c>cn) break;
s.pop_back();
if (s.empty()) s.push_back({{inf, i}, {mn, cn}});
} else {
int x=(c-cn)/(mn-m);
int j=s.back().first.second;
s.pop_back();
if (s.empty() || s.back().first.first<x) {
s.push_back({{x, j}, {m, c}});
s.push_back({{inf, i}, {mn, cn}});
break;
}
}
}
}
}
int m=0, j=0;
for (int i = 1; i <= n; ++i) {
if (dp[i][j%2]>m) m=dp[i][k%2], j=i;
} cout << m << '\n';
cout << j;
for (int i = 0; i < k-1; ++i) {
j=prev[j][k-i];
cout << ' ' << j;
} cout << '\n';
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... |