Problem:
Huge Knapsack Problem

You have N items that you want to put them into a knapsack. Item i has value vi and weight wi.

You want to find a subset of items to put such that:

The total value of the items is as large as possible.

The items have combined weight at most W, that is capacity of the knapsack.

Find the maximum total value of items in the knapsack.

Input

N W
v1 w1
v2 w2
:
vN wN

The first line consists of the integers N and W. In the following N lines, the value and weight of the i-th item are given.

Output

Print the maximum total values of the items in a line.

Constraints

1 ≤ N ≤ 40

1 ≤ vi ≤ 1015

1 ≤ wi ≤ 1015

1 ≤ W ≤ 1015

Sample Input 1

4 5
4 2
5 2
2 1
8 3

Sample Output 1

13

Sample Input 2

2 20
5 9
4 10

Sample Output 2

9

Solution in C# (judge verdict: Accepted)

using System;
using System.Linq;
using Pair = System.Collections.Generic.KeyValuePair<long, long>;

namespace DPL1_H
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = scan;
            int N = (int)t[0];
            long W = t[1];
            var w = new long[N];
            var v = new long[N];
            for (int i = 0; i < N; i++)
            {
                t = scan;
                w[i] = t[1];
                v[i] = t[0];
            }

            int n1 = N / 2;
            var ps = new Pair[1 << n1];
            for (int i = 0; i < 1 << n1; i++)
            {
                long sw = 0, sv = 0;
                for (int j = 0; j < n1; j++)
                {
                    if ((i >> j & 1) == 1)
                    {
                        sw += w[j];
                        sv += v[j];
                    }
                }
                ps[i] = new Pair(sw, sv);
            }
            Array.Sort(ps, (a,b) => { return a.Key.CompareTo(b.Key); });
            int m = 1;
            for (int i = 0; i < 1 << n1; i++)
            {
                if (ps[m - 1].Value < ps[i].Value) ps[m++] = ps[i];
            }
            ps = ps.Take(m).ToArray();

            long ans = 0;
            int n2 = N - n1;

            for (int i = 0; i < 1 << n2; i++)
            {
                long sw = 0, sv = 0;
                for (int j = 0; j < n2; j++)
                {
                    if ((i >> j & 1) == 1)
                    {
                        sw += w[n1 + j];
                        sv += v[n1 + j];
                    }
                }
                if (sw <= W)
                {
                    long tv = ps[UpperBound(ps, W - sw) - 1].Value;
                    ans = Math.Max(ans, sv + tv);
                }
            }

            Console.WriteLine(ans);
        }

        static long UpperBound(Pair[] arr, long val)
        {
            int low = 0, high = arr.Length;
            int mid;

            while (low < high)
            {
                mid = ((high - low) >> 1) + low;
                if (arr[mid].Key <= val) low = mid + 1;
                else high = mid;
            }
            return low;
        }
        static long[] scan { get { return Array.ConvertAll(Console.ReadLine().Split(), long.Parse); } }
    }
}