-
Notifications
You must be signed in to change notification settings - Fork 5
/
1251D.cpp
68 lines (66 loc) · 1.26 KB
/
1251D.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
65
66
67
68
//21171_somesh || asomesh999
//AC
#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;
int main()
{
io
int T;
cin>>T;
while(T--)
{
ll n, s;
cin >> n >> s;
vector<pii> arr(n);
fr(i, 0, n) {
cin >> arr[i].first >> arr[i].second;
}
auto func = [&] (ll x) {
ll total = 0;
ll count = 0;
vector<int> brr;
fr(i, 0, n) {
if (arr[i].second < x) {
total += arr[i].first;
} else if (arr[i].first >= x) {
total += arr[i].first;
count++;
} else {
brr.push_back(arr[i].first);
}
}
sort(brr.begin(), brr.end());
int rem = max(0LL, (n + 1) / 2 - count);
if (rem > brr.size()) {
return false;
}
total += (rem) * x;
for(int i = 0; i < brr.size() - rem; i++) {
total += brr[i];
}
return (total <= s);
};
ll low = 0, high = 1e12;
ll ans = -1;
while (low <= high) {
ll mid = (low + high) / 2;
if (func(mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << ans << '\n';
}
return 0;
}