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;
#define int long long
const int NM = 5e5;
int N, a[NM+5], b[NM+5], ans = 0;
queue <int> q;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++){
cin >> a[i] >> b[i];
if (a[i] < b[i]) q.push(i);
}
for (int i = 1; i <= N; i++){
if (a[i] <= b[i]) continue;
while (!q.empty() && a[i] > b[i]){
int j = q.front();
if (b[j]-a[j] <= a[i]-b[i]){
ans += abs(i-j)*(b[j]-a[j]);
a[i] -= b[j]-a[j];
a[j] = b[j];
q.pop();
}
else{
ans += abs(i-j)*(a[i]-b[i]);
a[j] += a[i]-b[i];
a[i] = b[i];
}
}
}
cout << ans;
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... |