-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd.cpp
64 lines (61 loc) · 1.63 KB
/
d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int N, a[100000], b[100000], id[100000];
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> N;
for(int i = 0; i < N; i++) eat >> a[i] >> b[i];
iota(id, id+N, 0);
sort(id, id+N, [](int x, int y){return b[x] < b[y];});
int e1 = N-1, e2 = 0;
int cnt = 0;
int ans = 0;
for(int i = 0; i < N; i++){
int cur1 = id[i];
int cur2 = id[e1];
if(i > e1) break;
if(i == e1){
int lft = a[cur1] - e2;
if(cnt >= b[cur1]){
cnt += lft;
ans += lft;
}else{
int rem = b[cur1] - cnt;
if(rem >= lft){
cnt += lft;
ans += 2 * lft;
}else{
cnt += lft;
ans += 2 * rem + (lft - rem);
}
}
break;
}
if(cnt >= b[cur1]){
cnt += a[cur1];
ans += a[cur1];
//moo << i << " "<< ans << " " << cnt << " " << e1 << " " << e2 << endl;
continue;
}
int rem = b[cur1] - cnt;
if(a[cur2] - e2 <= rem){
cnt += a[cur2] - e2;
ans += 2 * (a[cur2] - e2);
e1--;
e2 = 0;
i--;
}else{
cnt += rem;
ans += 2 * rem;
e2 += rem;
cnt += a[cur1];
ans += a[cur1];
}
//moo << i << " "<< ans << " " << cnt << " " << e1 << " " << e2 << endl;
}
moo << ans << endl;
}