이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |