-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmeetinthemiddle.cpp
More file actions
80 lines (76 loc) · 1.42 KB
/
meetinthemiddle.cpp
File metadata and controls
80 lines (76 loc) · 1.42 KB
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
#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;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define pb push_back
#define pi pair<int, int>
#define pii pair<pi, int>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 1000001
int n, t;
vector<int> v;
vector<int> a;
vector<int> b;
void solve2(int i, int j, int k)
{
if (i == j)
{
b.pb(k);
return;
}
solve2(i + 1, j, k);
solve2(i + 1, j, k + v[i]);
}
void solve(int i, int j, int k)
{
if (i == j)
{
a.pb(k);
return;
}
solve(i + 1, j, k);
solve(i + 1, j, k + v[i]);
}
int upper(int l, int r, int x)
{
while (l < r)
{
int mid = (l + r + 1) >> 1;
(b[mid] <= x) ? l = mid : r = mid - 1;
}
return b[l];
}
int meetinthemiddle()
{
solve(0, (n >> 1) + 1, 0);
solve2((n >> 1) + 1, n, 0);
sort(b.begin(), b.end());
int ans = 0;
for (auto const &i : a)
{
if (i > t)
continue;
ans = max(ans, i);
int kappa = i + upper(0, b.size() - 1, t - i);
if (kappa <= t)
ans = max(ans, kappa);
}
return ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> t;
v.resize(n);
for (int i = 0; i < n; i++)
cin >> v[i];
cout << meetinthemiddle() << endl;
return 0;
}