-
Notifications
You must be signed in to change notification settings - Fork 0
/
1539D.cpp
52 lines (44 loc) · 1002 Bytes
/
1539D.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
#include<bits/stdc++.h>
#define ll long long int
#define show(x) cout << #x << " : " << x << endl
using namespace std;
ll n,b,e,ans,sum;
vector<pair<ll,ll> > vp;
bool fn(ll pre)
{
ll need = sum-pre;
for (int i=0 ; i<n ; i++){
if (pre>=vp[i].first){
pre+=vp[i].second;
need-=vp[i].second;
}
else break;
}
if (need>0)return false;
return true;
}
int main()
{
cin >> n;
b = 0 , e = 0;
for (int i=0 ; i<n ; i++){
ll a,b;
cin >> a >> b;
vp.push_back({b,a});
e+=a;
}
sort(vp.begin(),vp.end());
ans = e*2+5,sum = e;
// show(sum);
while(b<=e){
ll mid = (b+e)/2;
if (fn(mid)){
// show(mid);
ans = min(ans,mid*2+(sum-mid));
e = mid-1;
}
else b = mid+1;
}
cout << ans << endl;
return 0;
}