#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
7 ms |
7284 KB |
Output is correct |
5 |
Correct |
12 ms |
7816 KB |
Output is correct |
6 |
Correct |
33 ms |
10584 KB |
Output is correct |
7 |
Correct |
72 ms |
16980 KB |
Output is correct |
8 |
Correct |
55 ms |
15184 KB |
Output is correct |
9 |
Correct |
53 ms |
14412 KB |
Output is correct |
10 |
Correct |
43 ms |
12436 KB |
Output is correct |
11 |
Correct |
40 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
7 ms |
7284 KB |
Output is correct |
5 |
Correct |
12 ms |
7816 KB |
Output is correct |
6 |
Correct |
33 ms |
10584 KB |
Output is correct |
7 |
Correct |
72 ms |
16980 KB |
Output is correct |
8 |
Correct |
55 ms |
15184 KB |
Output is correct |
9 |
Correct |
53 ms |
14412 KB |
Output is correct |
10 |
Correct |
43 ms |
12436 KB |
Output is correct |
11 |
Correct |
40 ms |
12116 KB |
Output is correct |
12 |
Incorrect |
19 ms |
8788 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |