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 pii = pair<int, int>;
using ll = long long;
vt<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int inf = 1e18, N = 1e6 + 5, mod = 1e9 + 7;
double eps = 1e-6;
bool fl = 0;
int ntr(pii a, pii b) {
int x = b.f - a.f;
int y = a.s - b.s;
return (x + y - 1) / y;
}
void solve() {
int n, k;
cin >> n >> k;
int p[n + 1];
pii pr[n + 1][k + 1];
p[0] = 0;
for (int i = 1, a; i <= n; i++) {
cin >> a;
p[i] = p[i - 1] + a;
}
int dp[n + 1][k + 1];
for (int i = 0; i <= n; i++) {
for (int s = 0; s <= k; s++) dp[i][s] = -inf;
}
dp[0][0] = 0;
vector<pair<pii, pii>> s;
s.pb({{-inf, -1}, {0, 0}});
for (int j = 0; j <= k; j++) {
int l = 0;
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[i][j] = max(dp[i - 1][j], s[l - 1].s.f + s[l - 1].s.s * p[i]);
if (dp[i][j] == dp[i - 1][j]) {
pr[i][j] = {i - 1, j};
}else {
pr[i][j] = {s[l - 1].f.s, j - 1};
}
}
s.clear();
for (int i = 1; i <= n; i++) {
int a = dp[i][j] - p[i] * p[i], b = p[i];
int ps = -inf;
if (sz(s) && b == s.back().s.s) {
if (a > s.back().s.f) {
s.pop_back();
while (sz(s) && ntr({a, b}, s.back().s) <= s.back().f.f) s.pop_back();
if (sz(s)) ps = ntr({a, b}, s.back().s);
s.pb({{ps, i}, {a, b}});
}
continue;
}
while (sz(s) && ntr({a, b}, s.back().s) <= s.back().f.f) s.pop_back();
if (sz(s)) ps = ntr({a, b}, s.back().s);
s.pb({{ps, i}, {a, b}});
}
}
cout << dp[n][k] << nl;
vector<int> ans;
int i = n, j = k;
while (j > 0) {
int i1 = pr[i][j].f, j1 = pr[i][j].s;
if (j1 == j) {
i = i1, j = j1;
continue;
}
ans.pb(i1);
i = i1, j = j1;
}
for (auto 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: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long 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:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | 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... |