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 <iostream>
#include <queue>
using namespace std;
#define N 500005
int n, a[N], b[N];
priority_queue<pair<int, int>> pq, qq;
long long cost;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
{
int a, b;
cin >> a >> b;
pq.emplace(i, a);
while (b and pq.size())
{
auto [j, aa] = pq.top(); pq.pop();
auto u = min(b, aa);
cost += 1ll*u*(i-j);
b -= u;
if (aa > u) pq.emplace(j, aa - u);
}
if (b)
{
qq.emplace(i, b);
}
while (pq.size() and qq.size())
{
auto [j, aa] = pq.top(); pq.pop();
auto [k, bb] = qq.top(); qq.pop();
auto u = min(aa, bb);
cost += 1ll*abs(j-k)*u;
if (aa > u) pq.emplace(j, aa - u);
if (bb > u) qq.emplace(k, bb - u);
}
}
cout<<cost;
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... |