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 pair <int, int> pii;
int a[1000010], b[1000010];
int main(int argc, char const *argv[])
{
ios_base :: sync_with_stdio (false);
cin.tie (0);
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
priority_queue <pii> P;
priority_queue <pii> Q;
long long ans = 0;
for(int i = 0; i < n; i++) {
int cur = a[i];
while(!P.empty() && cur > 0) {
int x = P.top().first;
int y = P.top().second;
P.pop();
long long match = min(cur, y);
ans += match * abs(i - x);
if(y > match) P.push(pii(x, y - match));
cur -= match;
}
if(cur > 0) Q.push(pii(i, cur));
cur = b[i];
while(!Q.empty() && cur > 0) {
int x = Q.top().first;
int y = Q.top().second;
Q.pop();
long long match = min(cur, y);
ans += match * abs(i - x);
if(y > match) Q.push(pii(x, y - match));
cur -= match;
}
if(cur > 0) P.push(pii(i, cur));
}
cout << ans << endl;
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... |