// Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ll long long
#define int long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl "\n"
#define x first
#define y second
#define gcd(a,b) __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define hell 1000000007
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}
int solve(){
int n;cin>>n;
priority_queue<int>pq;
int x,y;
vector<int>a(n+1);
rep(i,1,n+1){
cin>>x>>y;
a[i]=x-y;
a[i]+=a[i-1];
}
int ans=0;
rep(i,1,n+1){
if(a[i]<0){
ans-=a[i];
a[i]=0;
}
else if(a[i]>a[n]){
ans+=a[i]-a[n];
a[i]=a[n];
}
}
pq.push(a[1]);
rep(i,2,n){
int s=a[i];
auto itr=pq.top();
if(itr>s){
ans+=abs(itr-s);
pq.pop();
pq.push(s);
pq.push(s);
}
else{
pq.push(s);
}
}
cout<<ans<<endl;
return 0;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;//cin>>t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
9 ms |
1648 KB |
Output is correct |
5 |
Correct |
18 ms |
2924 KB |
Output is correct |
6 |
Correct |
60 ms |
7912 KB |
Output is correct |
7 |
Correct |
104 ms |
15204 KB |
Output is correct |
8 |
Correct |
91 ms |
13412 KB |
Output is correct |
9 |
Correct |
101 ms |
12772 KB |
Output is correct |
10 |
Correct |
77 ms |
10468 KB |
Output is correct |
11 |
Correct |
69 ms |
10468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
9 ms |
1648 KB |
Output is correct |
5 |
Correct |
18 ms |
2924 KB |
Output is correct |
6 |
Correct |
60 ms |
7912 KB |
Output is correct |
7 |
Correct |
104 ms |
15204 KB |
Output is correct |
8 |
Correct |
91 ms |
13412 KB |
Output is correct |
9 |
Correct |
101 ms |
12772 KB |
Output is correct |
10 |
Correct |
77 ms |
10468 KB |
Output is correct |
11 |
Correct |
69 ms |
10468 KB |
Output is correct |
12 |
Correct |
28 ms |
4108 KB |
Output is correct |
13 |
Correct |
68 ms |
10980 KB |
Output is correct |
14 |
Correct |
132 ms |
15204 KB |
Output is correct |
15 |
Correct |
112 ms |
13412 KB |
Output is correct |
16 |
Correct |
107 ms |
12772 KB |
Output is correct |
17 |
Correct |
87 ms |
10544 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
516 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
516 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
516 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
9 ms |
1648 KB |
Output is correct |
12 |
Correct |
18 ms |
2924 KB |
Output is correct |
13 |
Correct |
60 ms |
7912 KB |
Output is correct |
14 |
Correct |
104 ms |
15204 KB |
Output is correct |
15 |
Correct |
91 ms |
13412 KB |
Output is correct |
16 |
Correct |
101 ms |
12772 KB |
Output is correct |
17 |
Correct |
77 ms |
10468 KB |
Output is correct |
18 |
Correct |
69 ms |
10468 KB |
Output is correct |
19 |
Correct |
28 ms |
4108 KB |
Output is correct |
20 |
Correct |
68 ms |
10980 KB |
Output is correct |
21 |
Correct |
132 ms |
15204 KB |
Output is correct |
22 |
Correct |
112 ms |
13412 KB |
Output is correct |
23 |
Correct |
107 ms |
12772 KB |
Output is correct |
24 |
Correct |
87 ms |
10544 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
42 ms |
4204 KB |
Output is correct |
33 |
Correct |
98 ms |
10980 KB |
Output is correct |
34 |
Correct |
167 ms |
15204 KB |
Output is correct |
35 |
Correct |
156 ms |
12900 KB |
Output is correct |
36 |
Correct |
102 ms |
13412 KB |
Output is correct |
37 |
Correct |
152 ms |
15332 KB |
Output is correct |
38 |
Correct |
94 ms |
11492 KB |
Output is correct |
39 |
Correct |
128 ms |
10980 KB |
Output is correct |
40 |
Correct |
93 ms |
10492 KB |
Output is correct |
41 |
Correct |
94 ms |
10468 KB |
Output is correct |
42 |
Correct |
94 ms |
10468 KB |
Output is correct |
43 |
Correct |
96 ms |
10468 KB |
Output is correct |
44 |
Correct |
119 ms |
15332 KB |
Output is correct |
45 |
Correct |
90 ms |
10828 KB |
Output is correct |
46 |
Correct |
115 ms |
10492 KB |
Output is correct |
47 |
Correct |
1 ms |
492 KB |
Output is correct |