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 int long long
int MOD = 1e9 + 7;
const int N = 1e5;
int n, k, a[N], trace[N][205], ps[N];
vector<int> pre, cur;
void calc(int l, int r, int optl, int optr, int k) {
if(l > r) return;
pair<int, int> best = {-1, -1};
int mid = (l + r) >> 1;
for(int i = optl; i <= min(mid-1, optr); i++) {
best = max(best, {pre[i] + ps[i] * (ps[mid]-ps[i]), i});
}
trace[mid][k] = max(best.se, 1LL);
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);
for(int i = 1; i <= n; i++) trace[i][0] = 1;
for(int i = 1; i <= k; i++) {
calc(1, n, 1, n, i);
pre = cur;
}
cout << cur[n] << ln;
vector<int> ans;
int cur = n;
// for(int i = 1; i <= n; i++) cout << trace[i][1] << ' ';
cout << ln;
for(int i = k; i > 0; i--) {
ans.pb(trace[cur][i]);
cur = trace[cur][i] - 1;
}
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... |