Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1LL << 60;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, w;
  cin >> n >> w;
  vector<vector<ll>> dp(n + 1, vector<ll>(100010, INF));
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    dp[i + 1] = dp[i];
    for (int j = 0; j < 100010; ++j) {
      for (int k = 0; k <= v; ++k) {
        if (j + k >= 100010)
          break;
        dp[i + 1][j + k] = min(dp[i + 1][j + k], dp[i][j] + u);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 100010; ++i)
    if (dp[n][i] <= w)
      ans = i;
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1LL << 60;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, w;
  cin >> n >> w;
  vector<vector<ll>> dp(n + 1, vector<ll>(100010, INF));
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    dp[i + 1] = dp[i];
    for (int j = 0; j < 100010 - v; ++j) {
      dp[i + 1][j + v] = min(dp[i + 1][j + v], dp[i][j] + u);
    }
  }
  int ans = 0;
  for (int i = 0; i < 100010; ++i)
    if (dp[n][i] <= w)
      ans = i;
  cout << ans << endl;
  return 0;
}