This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
//#define int long long
#define pb push_back
#define vt vector
#define s second
#define f first
#define nl '\n'
using ll = long long;
using pii = pair<int, int>;
vt<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int inf = 1e9, N = 1e6 + 5, mod = 1e9 + 7;
double eps = 1e-6;
bool fl = 0;
ll ntr(pii a, pii b) {
ll x = b.f - a.f;
ll y = a.s - b.s;
return (x + y - 1) / y;
}
void solve() {
int n, k;
cin >> n >> k;
ll p[n + 1];
int pri[n + 1][k + 1], prj[n + 1][k + 1];
p[0] = 0;
for (int i = 1, a; i <= n; i++) {
cin >> a;
p[i] = p[i - 1] + a;
}
ll dp[2][k + 1];
for (int s = 1; s <= k; s++) dp[0][s] = -inf;
dp[0][0] = 0;
vt<pair<pii, pii>> s;
s.pb({{0, -1}, {0, 0}});
for (int j = 0; j <= k; j++) {
int l = 0;
vt<pair<pii, pii>> h;
for (int i = j + 1; i <= n; i++) {
while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++;
dp[1][j] = max(dp[0][j], s[l - 1].s.f + s[l - 1].s.s * p[i]);
if (dp[1][j] == dp[0][j]) {
pri[i][j] = i - 1;
prj[i][j] = j;
}else {
pri[i][j] = s[l - 1].f.s;
prj[i][j] = j - 1;
}
dp[0][j] = dp[1][j];
ll a = dp[1][j] - p[i] * p[i], b = p[i];
ll ps = 0;
if (sz(h) && b == h.back().s.s) {
if (a > h.back().s.f) {
h.pop_back();
while (sz(h) && ntr({a, b}, h.back().s) <= h.back().f.f) h.pop_back();
if (sz(h)) ps = ntr({a, b}, h.back().s);
h.pb({{ps, i}, {a, b}});
}
continue;
}
while (sz(h) && ntr({a, b}, h.back().s) <= h.back().f.f) h.pop_back();
if (sz(h)) ps = ntr({a, b}, h.back().s);
h.pb({{ps, i}, {a, b}});
}
swap(s, h);
}
cout << dp[0][k] << nl;
vt<int> ans;
int i = n, j = k;
while (j > 0) {
if (prj[i][j] == j) {
i = pri[i][j];
continue;
}
i = pri[i][j];
j--;
ans.pb(i);
}
for (int x: ans) cout << x << ' ';
}
main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int tp = 1;
if (fl) cin >> tp;
while (tp--) solve();
}
Compilation message (stderr)
sequence.cpp: In function 'void solve()':
sequence.cpp:55:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++;
| ^
sequence.cpp: At global scope:
sequence.cpp:97:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
97 | main() {
| ^~~~
# | 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... |