#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
int a[n], b[n];
map <int, int> mp;
map <int, int> mp1;
map <int, int> mp2;
map <int, int> mp3;
int p = 0;
int g = 0;
int ok = 0;
int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
37 ms |
5468 KB |
Output is correct |
5 |
Correct |
77 ms |
10576 KB |
Output is correct |
6 |
Correct |
98 ms |
11692 KB |
Output is correct |
7 |
Correct |
540 ms |
51304 KB |
Output is correct |
8 |
Incorrect |
638 ms |
51312 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
37 ms |
5468 KB |
Output is correct |
5 |
Correct |
77 ms |
10576 KB |
Output is correct |
6 |
Correct |
98 ms |
11692 KB |
Output is correct |
7 |
Correct |
540 ms |
51304 KB |
Output is correct |
8 |
Incorrect |
638 ms |
51312 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |