# Yuki 626 Randomized 01 Knapsack

Problem Description:
0/1 Knapsack problem. Test cases are generated at complete randomness.

Constraints:
2 ≤ N ≤ 5000
v[i], w[i] is uniformly distributed in range [1, 1e12]
1 ≤ W ≤ 1e12 * N

Solution:
Branch and Bound. Referenced kimiyuki's blog post.

Code:

```#include <bits/stdc++.h>

long long branch_and_bound(int n, long long c, const std::vector<long long> &v, const std::vector<long long> &w) {
std::vector<long long> vv(n), ww(n); {
std::vector<int> id(n);
std::iota(id.begin(), id.end(), 0);
std::sort(id.begin(), id.end(), [&](int i, int j) {
return 1.0 * v[i] * w[j] > 1.0 * v[j] * w[i];
});
for (int i = 0; i < n; ++i) {
vv[i] = v[id[i]];
ww[i] = w[id[i]];
}
}
long long ans = 0;
std::function<void(int, long long, long long)> dfs = [&](int x, long long sv, long long sw) {
if (c < sw) return;
if (x == n) return ans = std::max(ans, sv), void();
long long gsv = sv, gsw = sw;
int y = x;
for ( ; y < n && gsw + ww[y] <= c; ++y) {
gsv += vv[y];
gsw += ww[y];
}
if (gsw == c || y == n) return ans = std::max(ans, gsv), void();
if (gsv + 1.0 * vv[y] * (c - gsw) / ww[y] <= ans) return;
dfs(x + 1, sv + vv[x], sw + ww[x]);
dfs(x + 1, sv, sw);
};
dfs(0, 0, 0);
return ans;
}

signed main() {
std::ios::sync_with_stdio(false);
int N;
long long C;
std::cin >> N >> C;
std::vector<long long> V(N), W(N);
for (int i = 0; i < N; ++i) {
std::cin >> V[i] >> W[i];
}
std::cout << branch_and_bound(N, C, V, W) << std::endl;
return 0;
}
```