#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e16;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=2027865967;
const ll p=70032301;
const ull p2=913;
const int L=20;
int n;
ll t[N];
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
t[i]=t[i-1]+a-b;
}
multiset<ll> c;
c.insert(0);
ll res=0;
for(int i=1;i<n;i++){
auto it=c.end();
it--;
ll val=t[i];
if(t[i]<0) res+=abs(t[i]),val=0;
if((*it)>t[i]){
c.erase(it);
res+=(*it)-val;
c.insert(val),c.insert(val);
}else c.insert(val);
}
auto it2=c.end();
it2--;
auto it=c.end();
ll sl=0,pop=0;
for(it--;(*it)>t[n];it--,sl++){
res+=sl*(pop-*it);
pop=*it;
}
res+=sl*(pop-t[n]);
cout<<res<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
17 ms |
5208 KB |
Output is correct |
5 |
Correct |
39 ms |
7760 KB |
Output is correct |
6 |
Correct |
95 ms |
17768 KB |
Output is correct |
7 |
Correct |
214 ms |
34732 KB |
Output is correct |
8 |
Correct |
168 ms |
32852 KB |
Output is correct |
9 |
Correct |
197 ms |
32240 KB |
Output is correct |
10 |
Correct |
165 ms |
29920 KB |
Output is correct |
11 |
Correct |
146 ms |
30012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
17 ms |
5208 KB |
Output is correct |
5 |
Correct |
39 ms |
7760 KB |
Output is correct |
6 |
Correct |
95 ms |
17768 KB |
Output is correct |
7 |
Correct |
214 ms |
34732 KB |
Output is correct |
8 |
Correct |
168 ms |
32852 KB |
Output is correct |
9 |
Correct |
197 ms |
32240 KB |
Output is correct |
10 |
Correct |
165 ms |
29920 KB |
Output is correct |
11 |
Correct |
146 ms |
30012 KB |
Output is correct |
12 |
Correct |
50 ms |
10212 KB |
Output is correct |
13 |
Correct |
144 ms |
22628 KB |
Output is correct |
14 |
Correct |
216 ms |
34912 KB |
Output is correct |
15 |
Correct |
163 ms |
32848 KB |
Output is correct |
16 |
Correct |
278 ms |
32616 KB |
Output is correct |
17 |
Correct |
126 ms |
30052 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
17 ms |
5208 KB |
Output is correct |
12 |
Correct |
39 ms |
7760 KB |
Output is correct |
13 |
Correct |
95 ms |
17768 KB |
Output is correct |
14 |
Correct |
214 ms |
34732 KB |
Output is correct |
15 |
Correct |
168 ms |
32852 KB |
Output is correct |
16 |
Correct |
197 ms |
32240 KB |
Output is correct |
17 |
Correct |
165 ms |
29920 KB |
Output is correct |
18 |
Correct |
146 ms |
30012 KB |
Output is correct |
19 |
Correct |
50 ms |
10212 KB |
Output is correct |
20 |
Correct |
144 ms |
22628 KB |
Output is correct |
21 |
Correct |
216 ms |
34912 KB |
Output is correct |
22 |
Correct |
163 ms |
32848 KB |
Output is correct |
23 |
Correct |
278 ms |
32616 KB |
Output is correct |
24 |
Correct |
126 ms |
30052 KB |
Output is correct |
25 |
Correct |
1 ms |
504 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
43 ms |
9988 KB |
Output is correct |
34 |
Correct |
112 ms |
22624 KB |
Output is correct |
35 |
Correct |
216 ms |
34788 KB |
Output is correct |
36 |
Correct |
254 ms |
32336 KB |
Output is correct |
37 |
Correct |
161 ms |
33020 KB |
Output is correct |
38 |
Correct |
222 ms |
34916 KB |
Output is correct |
39 |
Correct |
165 ms |
30960 KB |
Output is correct |
40 |
Correct |
144 ms |
28632 KB |
Output is correct |
41 |
Correct |
153 ms |
29928 KB |
Output is correct |
42 |
Correct |
150 ms |
30012 KB |
Output is correct |
43 |
Correct |
159 ms |
29980 KB |
Output is correct |
44 |
Correct |
146 ms |
30052 KB |
Output is correct |
45 |
Correct |
256 ms |
34752 KB |
Output is correct |
46 |
Correct |
140 ms |
30480 KB |
Output is correct |
47 |
Correct |
130 ms |
25684 KB |
Output is correct |