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;
typedef long long ll;
#define int ll
multiset<int> form(multiset<int> s1, multiset<int> s2, int add1, int add2) {
multiset<int> s;
for (auto &x : s1) {
s.insert(x + add1);
}
for (auto &x : s2) {
s.insert(x + add2);
}
return s;
}
void print(multiset<int> s) {
cout << " : ";
for (auto &x : s) {
cout << x << " ";
}
cout << "\n";
}
int get(multiset<int> s, int a, int b) {
int n = (int) s.size();
vector<int> xs;
for (auto &it : s) {
xs.push_back(it);
}
for (int i = n - 1; i >= 0; i--) {
b += xs[i];
a--;
if (a == 0) {
return b;
}
}
assert(0);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
multiset<int> s1, s2;
int n, h, x, b = 0, add1 = 0, add2 = 0;
cin >> n >> h >> x;
s1.insert(x);
s2.insert(x);
b = -x;
b -= h * (n - 1) * n / 2;
for (int i = 2; i <= n; i++) {
add1 -= h;
add2 += h;
int cut = *s2.begin() + add2;
int x;
cin >> x;
b -= x;
if (x < cut) {
s1.insert(x - add1);
s1.insert(x - add1);
auto it = s1.end();
it--;
int val = *it + add1;
s1.erase(it);
s2.insert(val - add2);
} else {
s2.insert(x - add2);
s2.insert(x - add2);
auto it = s2.begin();
int val = *it + add2;
s2.erase(it);
s1.insert(val - add1);
}
}
multiset<int> s = form(s1, s2, add1, add2);
cout << get(s, (int) s.size() / 2, b) << "\n";
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |