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)

#include<stdio.h>
#include<stdlib.h>

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

typedef struct item {
  int64 v,w;
} item;

int cmp(item a,item b){
  if(a.w!=b.w) return a.w<b.w?-1:1;
  return a.v>b.w?-1:1;
}

item* enumerate(item *a,int n){
  item *p=(item *)calloc(1<<n,sizeof(item));
  item *q=(item *)calloc(1<<n,sizeof(item));
  int i;
  for(i=0;i<n;i++){
    int l=0;
    int r=0;
    int len=0;
    while(l<(1<<i)){
      item t={p[r].v+a[i].v,p[r].w+a[i].w};
      if(cmp(p[l],t)<0){
	q[len++]=p[l++];
      } else {
	q[len++]=t;
	r++;
      }
    }
    while(r<(1<<i)){
      q[len++]=(item){p[r].v+a[i].v,p[r].w+a[i].w};;
      r++;
    }
    item *swap=p;
    p=q;
    q=swap;
  }
  free(q);
  for(i=1;i<(1<<n);i++) p[i].v=MAX(p[i].v,p[i-1].v);
  return p;
}

void run(void){
  int n;
  int64 w;
  scanf("%d%lld",&n,&w);
  item p[40];
  int i;
  for(i=0;i<n;i++) scanf("%lld%lld",&p[i].v,&p[i].w);
  int m=n/2;
  item *a=enumerate(p,m);
  item *b=enumerate(p+m,n-m);
  i=0;
  int j=(1<<(n-m))-1;
  int64 max=0;
  while(i<(1<<m) && j>=0){
    if(a[i].w+b[j].w<=w){
      max=MAX(max,a[i].v+b[j].v);
      i++;
    } else {
      j--;
    }
  }
  printf("%lld\n",max);
}

int main(void){
  run();
  return 0;
}