Problem:
Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)



#include <bits/stdc++.h>
using namespace std;


// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int               long long
#define endl              '\n'
#define ff                first
#define ss                second
#define mp                make_pair
#define pb                push_back
#define vii               vector<int> 
#define pii               pair<int,int>
#define vpii              vector< pii >
#define all(v)            v.begin(),v.end()
#define loop(i,s,e)       for(int i=s;i<e;i++)
#define rloop(i,e,s)      for(int i=e;i>=s;i--)
#define mset(a,f)              memset(a,f,sizeof(a))
#define M                 1000000007
#define fastio            ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define prvec(v)          loop(i_u_j,0,v.size())cout << v[i_u_j] <<" "; cout << endl;
#define prarr(arr,n)      loop(i_u_j,0,n)cout << arr[i_u_j] << " "; cout << endl;
#define bug(...)        __f (#__VA_ARGS__, __VA_ARGS__)


template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
const int N = 107;
int n;
pii arr[N];
int  dp[N][100007];
int pmax[N];

int getval(int it, int v, int mxw)
{                 
      // bug(it,v,mxw);
      if( mxw == 0 || it == n ) return dp[it][v] = 0;
      int pw = pmax[ it ];
      if( dp[it][v] != -1 and pw >= mxw ) 
      {     
            // cout << "already "; bug(it,v,mxw,pw);
            return dp[it][v];
      }
      
      if( pmax[ it ] <= mxw ) pmax[ it ] = mxw;
      int a = 0, b = 0;
      if( arr[it].ff <= mxw )
      {     
            a = arr[it].ss + getval(it+1, v + arr[it].ss,  mxw - arr[it].ff );
      }     
      b = getval(it+1, v, mxw);
      // bug(a,b);
      return dp[it][v] = max(dp[it][v], max(a,b));
}

bool comp( pii &p1, pii &p2)
{
      if( p1. ff < p2.ff ) return 1;
      else if( p1.ff > p2.ff ) return 0;
      return p1.ss > p2.ss;
}
void solve()
{     
      int mxw;
      cin >> n >> mxw;
      loop(i,0,n) cin >> arr[i].ff;
      loop(i,0,n) cin >> arr[i].ss;
      // loop(i,0,n)
      // {
      //       cin >> arr[i].ff >> arr[i].ss;
      // }
      // sort(arr,arr+n, comp);
      mset(dp,-1);
      mset(pmax,-1);
      cout << getval(0, 0, mxw); return;
   
}

int32_t main() {
    fastio;

  
  int t = 1; 
  //cin>>t;
  
  while(t--) solve();
  
  


}


// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //