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 ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define out(file) freopen(file, "w", stdout)
#define in(file) freopen(file, "r", stdin)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
int MOD = 1e9 + 7;
const int N = 1e5 + 7;
int n, k, a[N], ps[N];
vector<ll> pre, cur;
vector<vector<int>> trace;
void calc(int l, int r, int optl, int optr, int k) {
if(l > r) return;
pair<ll, int> best = {-inf, -1};
int mid = (l + r) >> 1;
for(int i = optl; i <= min(mid-1, optr); i++) {
best = max(best, {pre[i] + 1LL * ps[i] * (ps[mid]-ps[i]), i});
}
trace[mid][k] = best.se;
cur[mid] = best.fi;
calc(l, mid-1, optl, best.se, k);
calc(mid+1, r, best.se, optr, k);
}
signed main() {
fast_cin();
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i], ps[i] = ps[i-1] + a[i];
pre.resize(n+1, 0);
cur.resize(n+1, 0);
trace.resize(n+1, vector<int>(k+1, 0));
for(int i = 1; i <= k; i++) {
calc(i+1, n, i-1, n, i);
pre = cur;
}
cout << cur[n] << ln;
vector<int> ans;
int cur = n;
for(int i = k; i > 0; i--) {
assert(cur > 0);
ans.pb(trace[cur][i]);
cur = trace[cur][i];
}
reverse(all(ans));
for(int x : ans) cout << x << ' ';
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | 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... |