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;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
long long n;
cin >> n;
long long a[n], b[n];
map <long long, long long> mp;
map <long long, long long> mp1;
map <long long, long long> mp2;
map <long long, long long> mp3;
long long p = 0;
long long g = 0;
long long ok = 0;
long long okk = 0;
long long cnt = 0;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
if (a[i] >= b[i]) {
a[i] -= b[i];
b[i] = 0;
}
else {
b[i] -= a[i];
a[i] = 0;
}
if (a[i] > 0) {
mp2[okk] = a[i];
mp3[okk] = i;
okk++;
}
if (b[i] > 0) {
mp[ok] = b[i];
mp1[ok] = i;
ok++;
}
}
for (int j = 0; j < ok; j++) {
for (int i = g; i < okk; i++) {
if (mp[j] < mp2[i]) {
cnt += mp[j]*abs(mp1[j]-mp3[i]);
mp2[i] -= mp[j];
mp[j] = 0;
break;
}
else {
cnt += mp2[i]*abs(mp1[j]-mp3[i]);
mp[j] -= mp2[i];
mp2[i] = 0;
p++;
if (mp[j] == mp2[i]) {
break;
}
}
}
g += p;
p = 0;
}
cout << cnt;
}
# | 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... |