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