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;
constexpr int NMAX = 5e5 + 5;
typedef long long LL;
struct SlopeTrick {
LL a, b;
priority_queue <LL> *H;
SlopeTrick () {
a = b = 0;
H = new priority_queue<LL>;
}
SlopeTrick operator += (SlopeTrick other) {
a += other.a;
b += other.b;
if (H->size() > other.H->size())
swap(H, other.H);
while (!other.H->empty()) {
H->push(other.H->top());
other.H->pop();
}
while (a > 0) {
-- a;
b += H->top();
H->pop();
}
return *this;
}
SlopeTrick Evaluate () {
while (a > 0) {
-- a;
b += H->top();
H->pop();
}
return *this;
}
};
int N;
LL D[NMAX];
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 1; i <= N; ++ i ) {
LL A, B;
cin >> A >> B;
D[i] = D[i-1] + A - B;
}
}
void Solve () {
SlopeTrick ans;
for (int i = 1; i < N; ++ i ) {
SlopeTrick aux;
aux.a = 1;
aux.b = -D[i];
if (D[i] < 0) D[i] = 0;
aux.H->push(D[i]);
aux.H->push(D[i]);
ans += aux;
}
ans.Evaluate();
cout << ans.b << '\n';
}
int main () {
Read();
Solve();
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... |