#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define nl '\n'
#define tb '\t'
#define sp ' '
using namespace std;
const int MX=1005, MOD=998244353, BLOCK=327, INF=1e9+7;
const ll INFF=1e18+7;
const ld ERR=1e-7, pi=3.14159265358979323846;
ll N, ans=0, A, B, tot;
int main(){
fastio;
cin >> N;
queue<pll> pos, neg;
for(int i=1; i<=N; i++){
cin >> A >> B;
tot=B-A;
if(tot<0){
tot=-tot;
while(pos.size() && tot){
ll &idx=pos.front().fi, &val=pos.front().se;
if(val<=tot){
ans+=(i-idx)*val;
tot-=val;
pos.pop();
}else{
ans+=(i-idx)*tot;
val-=tot;
tot=0;
}
}
if(tot)
neg.push({i, tot});
}else if(tot>0){
while(neg.size() && tot){
ll &idx=neg.front().fi, &val=neg.front().se;
if(val<=tot){
ans+=(i-idx)*val;
tot-=val;
neg.pop();
}else{
ans+=(i-idx)*tot;
val-=tot;
tot=0;
}
}
if(tot)
pos.push({i, tot});
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
8 ms |
576 KB |
Output is correct |
5 |
Correct |
20 ms |
920 KB |
Output is correct |
6 |
Correct |
64 ms |
3620 KB |
Output is correct |
7 |
Correct |
90 ms |
6960 KB |
Output is correct |
8 |
Correct |
81 ms |
9296 KB |
Output is correct |
9 |
Correct |
89 ms |
4524 KB |
Output is correct |
10 |
Correct |
52 ms |
3836 KB |
Output is correct |
11 |
Correct |
55 ms |
2768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
8 ms |
576 KB |
Output is correct |
5 |
Correct |
20 ms |
920 KB |
Output is correct |
6 |
Correct |
64 ms |
3620 KB |
Output is correct |
7 |
Correct |
90 ms |
6960 KB |
Output is correct |
8 |
Correct |
81 ms |
9296 KB |
Output is correct |
9 |
Correct |
89 ms |
4524 KB |
Output is correct |
10 |
Correct |
52 ms |
3836 KB |
Output is correct |
11 |
Correct |
55 ms |
2768 KB |
Output is correct |
12 |
Incorrect |
25 ms |
1944 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |