-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1119B.cpp
195 lines (173 loc) · 8.75 KB
/
1119B.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// @Author: Mars_Coder
// @date: 24-08-2024 Sat 12:24 PM
// https://codeforces.com/problemset/problem/1119/B
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using i64 = long long int;
using uInt = unsigned int;
using ui64 = unsigned long long int;
using vi = vector<int>;
using vii = vector<i64>;
using vc = vector<char>;
using udouble = long double;
using vd = vector<double>;
using vs = vector<string>;
template<typename T> using vv = vector<vector<T>>;
template<typename T> using pq = priority_queue<T>; // high prec., pq<T> p;
template<typename T> using pq_ = priority_queue<T, vector<T>, greater<T>>; // dec. as: pq_<T> x;
template<typename T1, typename T2> using vp = vector<pair<T1, T2>>; // vp<T1, T2> ..
/*---------- pbds -------------*/
// *oset.find_by_order(idx ∈[0, n - 1]), oset.order_of_key(ai) + set/map fns
// order_of_key(ai) -> no of elements less than ai
// ordered_set/map:
/*
pbds<int, null_type, less<int>> uset; -> ordered_set
pbds<int, null_type, greater<int>> -> decr. order
pbds<int, null_type, less_equal<int>> -> multi-set(increasing)
pbds<int, null_type, greater_equal<int>> -> multi-set(decreasing)
pbds<int, int, less<int>> -> ordered_map
pbds<int, int, less_equal<int>> -> multi-map
............
!! ALERT !! (for using less_equals<key>)
using less_equals<key> makes lower_bound works as upper_bound and vice-versa
for erase use: any.erase(any.find_by_order(any.order_of_key(val)));
don't use .find() because it will always return .end()
*/
template<typename key, typename val = null_type, typename cmp = less<key>>
using pbds = tree<key, val, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/*-----------------------------*/
#define ln "\n" // no flush, oppos of endl
#define _flush endl
#define stop_sync ios::sync_with_stdio(false) // not to use c-style io
#define untie_ios cin.tie(nullptr) // no flush
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define Fi(p) get<0>(p)
#define Sc(p) get<1>(p)
#define sz(x) int ((x).size())
#define All(x) (x).begin(), (x).end()
#define scanv(x) for(auto &i: x) cin >> i
#define bin_sc(a, x) binary_search(All(a), x) // 0/1
#define lbd(a, x) lower_bound(All(a), x) // an iter.
#define ubd(a, x) upper_bound(All(a), x) // an iter.
#define eq_seg(a, x) equal_range(All(a), x) // a pair of lb, ub
#define minE(a) (*min_element(All(a)))
#define maxE(a) (*max_element(All(a)))
#define FIXED(x) cout << fixed << setprecision(x)
#define mem(a, v) memset(a, v, sizeof a) // 0,-1 for int, and all char
#define filla(a, n, v) fill(a, a + n, v) // for arr.
#define fillv(a, v) fill(All(a), v) // for vec.
#define _fillv(a, n, v) fill_n(a.begin(), n, v)
#define glue(x, y) x##y
#define msg(x) cout << (#x) << ln
#define bug(x) cout << (#x) << ": " << (x) << ln
#define printv(v) cout << "[ ";for(auto j: v) cout << j << ' '; cout << "]" << ln
#define printvv(v) cout << "["; for(auto i: v) {printv(i);} cout << "........]\n"
#define printm(m) cout << "[\n";for(auto i: m) cout << Fi(i) << " -> " << Sc(i) << ln; cout << "...]\n"
#define prints(s) cout << "{"; for(auto i: s) cout << i << ' '; cout << "}\n"
#define uceil(a, b) ((a + b - 1) / (b))
#define unq_v(a) a.resize(distance(a.begin(), unique(All(a)))) // for same valued consec. grp
#define mk_upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define mk_lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define valid(nx, ny) (nx >= 1 && nx <= row && ny >= 1 && ny <= col)
#define dbug(args...) {string s = #args;replace(All(s), ',', ' '); stringstream s2; s2 << s; vs ss;\
while(s2){string now; s2 >> now; ss.pb(now);} debug(ss, args);} // string sucks :(, don't know why!
vector<int> dx = {1,-1,0,0,1,-1,-1,1}; // (4 ed. + 4 dia.)
vector<int> dy = {0,0,1,-1,1,1,-1,-1}; // first four are ed.
const string yms[]{"YES", "Yes", "yes"};
const string nms[]{"NO", "No", "no"};
const long double PI = acos(-1.0L);
struct{const int i = (1e9) + 7; const i64 l = (i64)(1e9) + 7ll;}MOD;
struct{const int i = INT_MAX; const i64 l = LLONG_MAX;}inf;
const auto start_time = chrono::high_resolution_clock::now();
void _timer_(){
const auto end_time = std::chrono::high_resolution_clock::now();
double delta = (double) chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
FIXED(2);
cout << "[time: "<< delta <<" ms]\n";
}
bool isUp(char ch){locale loc; return isupper(ch, loc);}
void debug(vs s) {cout << "#------------------#\n";if(sz(s)){};}
int ffs(int x){return __builtin_ffs(x);} // 1 based idx from right of one
int ffs(i64 x){return __builtin_ffsll(x);}
int clz(uInt x){return __builtin_clz(x);} // leading zeros from left ....
int clz(ui64 x){return __builtin_clzll(x);}
int ctz(uInt x){return __builtin_ctz(x);} // trailing zeros from right ....
int ctz(ui64 x){return __builtin_ctzll(x);}
int cto(uInt x){return __builtin_popcount(x);} // no of 1 bit
int cto(ui64 x){return __builtin_popcountll(x);}
int parity(uInt x){return __builtin_parity(x);} // cto % 2
int parity(ui64 x){return __builtin_parityll(x);}
auto pow2(uInt x){return (1u) << x;} /* returning unsigned */
auto pow2(ui64 x){return (1ull) << x;} // x <= 63
i64 binpow(i64 a, i64 b){i64 ans = 1ll; while(b > 0){if(b & 1) ans *= a; a *= a; b >>= 1;} return ans;}
i64 binpow(i64 a, i64 b, i64 m){a %= m; i64 ans = 1ll; while(b > 0){if(b & 1) ans = ans * a % m; a = a * a % m; b >>= 1;} return ans;}
i64 NcR(i64 n, i64 r){i64 x = 1ll, y = 1ll;if(n - r < r) r = n - r; while(r){x *= n;y *= r; i64 cm = gcd(x, y);x /= cm;y /= cm;--n;--r;}return x;}
i64 lcm(i64 a, i64 b){ if(a > b) swap(a, b); return (a * (b / gcd(a, b)));}
i64 NpR(i64 n, i64 r){i64 x = 1ll;while(r){x *= n; --n;--r;}return x;}
int XOR1toN(int N){ int md = N % 4; return md == 0? N:(md == 3? 0:(md == 2? N + 1:1));} // O(1) T.C.
int XORLtoR(int L, int R){return XOR1toN(R) ^ XOR1toN(max(L - 1, 0));} // xor from range L to R - O(1)
template<typename T> T getMod(T a, T b){assert(("b = 0", b != 0));return ((a % b) + b) % b;}//a ∈ Z, b ∈ Z
template<typename type> type Nsum(type n){return (n * (n + 1)) / (type)2;}
template<typename T> T LOG(T base, T power){return log2(power) / log2(base);}
template<typename type> type Round(type a, type b) {if(a < b) return 1; if(a % b) return 1 + a / b; return a / b;}
template<typename T, typename... param> void debug(vs ss, T a, param... args){
cout << ss.front() << " = " << a << '\n'; ss.erase(ss.begin()); debug(ss, args...);
}
//--------- overloaded << operator to print stl containers.. cout << container << ln
// for vector(1D, 2D), vector of pair & set....
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v){out << "[";size_t last = v.size() - 1;for(size_t i = 0; i < v.size(); ++i){out << v[i];if (i != last)out << ", ";}out << "]";return out;}
// for set & set of ....
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v){os << "{";for(auto it : v){os << it;if(it != *v.rbegin()) os << ", ";}os << "}";return os;}
// for map, map of vector & .....
template <typename T, typename S> ostream& operator<<(ostream& os, const map<T, S>& v){for(auto it : v) os << it.first << " : " << it.second << "\n";return os;}
// for pair & pair of .......
template <typename T, typename S> ostream& operator<<(ostream& os, const pair<T, S>& v){os << "("; os << v.first << ", " << v.second << ")";return os;}
// array<T, n> a = {}
int main(void){
#ifdef ONPC
cout << "========================== compilation done ==========================\n";
#endif
stop_sync;
untie_ios;
int t(1), tcase(0);
//cin >> t;
while(++tcase, t--){
i64 n, h;
cin >> n >> h;
vector<i64> a(n);
for(auto &i: a) cin >> i;
i64 l = 1, r = n + 1, mid;
while(r > l + 1) {
mid = l + (r - l) / 2;
vector<i64> b;
b.assign(begin(a), begin(a) + mid);
sort(begin(b), end(b), greater<int>());
i64 tm = h;
for(i64 i = 0; i < sz(b); i += 2) {
tm -= b[i];
}
if(tm >= 0) {
l = mid;
}else {
r = mid;
}
b.clear();
}
cout << l << ln;
/*#ifdef ONPC
cout << "[testcase: " << tcase << "] ~~~~~~~~~~~~~~~~~~~~~~~~~ ";
_timer_();
#endif*/
}
#ifdef ONPC
cout << "\nfinished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
}