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;
using ll = long long;
using ld = long double;
ll dp[2][100005], ps[100005];
int p[205][100005];
struct line {
ll idx, m, c;
ll cal (ll x) {return m*x + c;}
ld sectx (line l) {return (ld) (l.c-c) / (m-l.m);}
};
int main () {
int n, k, a;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a, ps[i] = ps[i-1] + a;
deque<line> d;
for (int j = 1; j <= k; j++) {
d.push_back({0, 0, 0});
for (int i = 1; i <= n; i++) {
while ((int) d.size() >= 2 && d.back().cal(ps[i]) <= d.end()[-2].cal(ps[i])) d.pop_back();
dp[j&1][i] = d.back().cal(ps[i]) + ps[i]*ps[n] - ps[i]*ps[i];
p[j][i] = d.back().idx;
line now = {i, ps[i], dp[~j&1][i] - ps[i]*ps[n]};
while ((int) d.size() >= 2 && ((now.m == d[0].m && now.c >= d[0].c) || now.sectx(d[0]) <= d[0].sectx(d[1]))) d.pop_front();
d.push_front(now);
}
d.clear();
}
auto iter = max_element(dp[k&1]+k, dp[k&1]+n+1);
cout << *iter << "\n";
vector<int> path {iter-dp[k&1]};
for (int i = k; i > 0; i--) path.push_back(p[i][path.back()]);
for (int i = k-1; i >= 0; i--) cout << path[i] << " ";
cout << "\n";
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:34:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
34 | vector<int> path {iter-dp[k&1]};
| ~~~~^~~~~~~~
sequence.cpp:34:24: warning: narrowing conversion of '((iter - ((ll*)(& dp[(k & 1)]))) <unknown operator> 8)' from 'long int' to 'int' [-Wnarrowing]
# | 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... |