Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define feach(it, l) for (auto it = l.begin(); it != l.end(); ++it)
#define fitr(it, it1, itr2) for (auto it = itr1; it != itr2; ++it)
#define fall(a) a.begin(), a.end()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef vector<vector<ll>> vvll;
typedef vector<vector<int, int>> vvin;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<int, int>> vpin;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef queue<int> qin;
typedef priority_queue<int> pqin;
typedef map<ll, ll> mpll;
typedef map<int, int> mpin;
typedef pair<ll, ll> pll;
typedef pair<int, int> pin;
const ll MOD = 1000000007;
const long double PI = 3.1415926535897932384626433832795;
ll pwr(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      res *= b;
      p--;
    } else {
      b *= b;
      p >>= 1;
    }
  }
  return res;
}
const int maxn = 1e3 + 1, maxn1 = 101;
ll dp[maxn][maxn1];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vpll A(n + 1);
  F(i, 1, n + 1, 1) {
    ll a, b;
    cin >> a >> b;
    A[i] = mkp(a, b);
  }
  ll sum = 0;
  F(i, 1, n + 1, 1) sum += A[i].ss;
  F(i, 0, n + 1, 1) dp[0][i] = 0;
  F(i, 1, sum + 1, 1) dp[i][0] = INT_MAX;
  F(i, 1, n + 1, 1) {
    F(j, 1, sum + 1, 1) {
      dp[j][i] = dp[j][i - 1];
      if (j >= A[i].ss) {
        if (dp[j - A[i].ss][i - 1] <= w)
          dp[j][i] = min(dp[j][i], dp[j - A[i].ss][i - 1] + A[i].fs);
      }
    }
  }
  ll ans;
  F(i, 1, sum + 1, 1) {
    if (dp[i][n] <= w)
      ans = i;
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define feach(it, l) for (auto it = l.begin(); it != l.end(); ++it)
#define fitr(it, it1, itr2) for (auto it = itr1; it != itr2; ++it)
#define fall(a) a.begin(), a.end()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef vector<vector<ll>> vvll;
typedef vector<vector<int, int>> vvin;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<int, int>> vpin;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef queue<int> qin;
typedef priority_queue<int> pqin;
typedef map<ll, ll> mpll;
typedef map<int, int> mpin;
typedef pair<ll, ll> pll;
typedef pair<int, int> pin;
const ll MOD = 1000000007;
const long double PI = 3.1415926535897932384626433832795;
ll pwr(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      res *= b;
      p--;
    } else {
      b *= b;
      p >>= 1;
    }
  }
  return res;
}
const int maxn = 1e5 + 1, maxn1 = 101;
ll dp[maxn][maxn1];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vpll A(n + 1);
  F(i, 1, n + 1, 1) {
    ll a, b;
    cin >> a >> b;
    A[i] = mkp(a, b);
  }
  ll sum = 0;
  F(i, 1, n + 1, 1) sum += A[i].ss;
  F(i, 0, n + 1, 1) dp[0][i] = 0;
  F(i, 1, sum + 1, 1) dp[i][0] = INT_MAX;
  F(i, 1, n + 1, 1) {
    F(j, 1, sum + 1, 1) {
      dp[j][i] = dp[j][i - 1];
      if (j >= A[i].ss) {
        if (dp[j - A[i].ss][i - 1] <= w)
          dp[j][i] = min(dp[j][i], dp[j - A[i].ss][i - 1] + A[i].fs);
      }
    }
  }
  ll ans;
  F(i, 1, sum + 1, 1) {
    if (dp[i][n] <= w)
      ans = i;
  }
  cout << ans;

  return 0;
}