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;

namespace DPL1_H
{
    class Program
    {
        static long W;
        static int n;
        static long[] w = new long[200];
        static long[] v = new long[200];

        static void Main(string[] args)
        {
            solve();
        }
        static void solve()
        {
            var t = scan;
            n = (int)t[0];
            W = t[1];
            for (long i = 0; i < n; i++)
            {
                var j = scan;
                w[i] = j[1];
                v[i] = j[0];
            }
            data1();
        }
        static void data1()
        {
            int n1 = n / 2;
            var ps = new pair<long, long>[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<long, long>(sw, sv);
            }
            Array.Sort(ps);
            int m = 1;
            for (int i = 0; i < 1 << n1; i++)
            {
                if (ps[m - 1].Second < ps[i].Second) ps[m++] = ps[i];
            }
            ps = ps.Take(m).ToArray();

            long res = 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[UpperBound1(ps, W - (long)sw) - 1].Second;
                    res = Math.Max(res, sv + tv);
                }
            }
            Console.WriteLine(res);
        }
   

        static long UpperBound1(pair<long, long>[] arr, long val)
        {
            long low = 0, high = arr.Count();
            long mid;

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

    public class pair<T, U> : IComparable where T : IComparable<T>
    {
        public pair()
        {
        }

        public pair(T first, U second)
        {
            this.First = first;
            this.Second = second;
        }

        public int CompareTo(Object obj)
        {
            pair<T, U> cast = (pair<T, U>)obj;
            return First.CompareTo(cast.First);
        }

        public T First { get; set; }

        public U Second { get; set; }

        public override string ToString()
        {
            return string.Format("{0} {1}?\n", First, Second);
        }

    }
}