Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define maxn 10000000000000
#define endl '\n'
#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
int mod(int x, int y) {
  int s;
  s = abs(x) / y;
  if (x < 0)
    s--;
  return x - y * s;
}
int dir8[2][8] = {{1, -1, 0, 0, 1, -1, 1, -1}, {0, 0, 1, -1, 1, -1, -1, 1}};
int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
ll m, n, row, col, k;
ll memo[105][1005];
ll cost[105], weight[105];
ll maxVal = 0;
void solve() {
  scanf("%lld%lld", &n, &m);
  for (int w = 0; w < n; w++) {
    scanf("%lld%lld", &weight[w], &cost[w]);
    maxVal += cost[w];
  }
  for (int w = 0; w <= maxVal; w++)
    memo[n - 1][w] = (cost[n - 1] >= w) ? weight[n - 1] : maxn;
  for (int i = n - 2; i >= 0; i--) {
    for (ll j = 0; j <= maxVal; j++) {
      if (cost[i] >= j) {
        memo[i][j] = min(memo[i + 1][j], weight[i]);
      } else {
        memo[i][j] = min(weight[i] + memo[i + 1][j - cost[i]], memo[i + 1][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= maxVal; i++) {
    //  cout << memo[0][i] << " " << i << endl;
    ans = (memo[0][i] <= m) ? i : ans;
  }
  printf("%lld\n", ans);
}
void test() {
  int t;
  // scanf("%d",&t);
  t = 1;
  for (int test = 1; test <= t; test++) {
    // memset(memo,-1,sizeof memo);
    solve();
  }
}
int main() {
  test();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define maxn 10000000000000
#define endl '\n'
#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
int mod(int x, int y) {
  int s;
  s = abs(x) / y;
  if (x < 0)
    s--;
  return x - y * s;
}
int dir8[2][8] = {{1, -1, 0, 0, 1, -1, 1, -1}, {0, 0, 1, -1, 1, -1, -1, 1}};
int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
ll m, n, row, col, k;
ll memo[105][100005];
ll cost[105], weight[105];
ll maxVal = 0;
void solve() {
  scanf("%lld%lld", &n, &m);
  for (int w = 0; w < n; w++) {
    scanf("%lld%lld", &weight[w], &cost[w]);
    maxVal += cost[w];
  }
  for (int w = 0; w <= maxVal; w++)
    memo[n - 1][w] = (cost[n - 1] >= w) ? weight[n - 1] : maxn;
  for (int i = n - 2; i >= 0; i--) {
    for (ll j = 0; j <= maxVal; j++) {
      if (cost[i] >= j) {
        memo[i][j] = min(memo[i + 1][j], weight[i]);
      } else {
        memo[i][j] = min(weight[i] + memo[i + 1][j - cost[i]], memo[i + 1][j]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= maxVal; i++) {
    //  cout << memo[0][i] << " " << i << endl;
    ans = (memo[0][i] <= m) ? i : ans;
  }
  printf("%lld\n", ans);
}
void test() {
  int t;
  // scanf("%d",&t);
  t = 1;
  for (int test = 1; test <= t; test++) {
    // memset(memo,-1,sizeof memo);
    solve();
  }
}
int main() {
  test();
  return 0;
}