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 <iostream>
#include <tuple>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using seq = tuple<ll, int, int, int>;
const int N = 1e5;
int n, k;
ll a[N + 5], pfx_sum[N + 5];
seq eval(int L, int R) {
if (L == R) return make_tuple(0, L, R, L);
int l = L, r = R;
while (r - l > 1) {
int m = (l + r) / 2;
if (2 * pfx_sum[m] <= pfx_sum[R] - pfx_sum[L - 1]) l = m + 1;
else r = m;
}
if (2 * (pfx_sum[l] - pfx_sum[L - 1]) <= pfx_sum[R] - pfx_sum[L - 1]) l++;
r = l;
l--;
ll x1 = (pfx_sum[l] - pfx_sum[L - 1]) * (pfx_sum[R] - pfx_sum[l]);
ll x2 = (pfx_sum[r] - pfx_sum[L - 1]) * (pfx_sum[R] - pfx_sum[r]);
if (x1 < x2) return make_tuple(x2, L, R, r);
else return make_tuple(x1, L, R, l);
}
ll solve() {
priority_queue<seq> q;
q.push(eval(1, n));
ll ret = 0;
while (k--) {
auto [score, l, r, m] = q.top();
q.pop();
ret += score;
q.push(eval(l, m));
q.push(eval(m + 1, r));
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
pfx_sum[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pfx_sum[i] = a[i] + pfx_sum[i - 1];
}
cout << solve() << '\n';
return 0;
}
# | 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... |