Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimze("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ";                                          \
  __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...);
}
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld", &x)
ll t = 1, tt;
const ll inf = 1e18, N = 100005;
ll dp[100][N];
inline ll mult(ll x, ll y) {
  x %= hell;
  y %= hell;
  return (1LL * x * y) % hell;
}
inline ll add(ll x, ll y) {
  x += y;
  return x % hell;
}
inline ll powMod(ll base, ll expo) {
  if (base == 0)
    return 0;
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
void solve() {
  memset(dp, -1, sizeof dp);
  ll n, k;
  cin >> n >> k;
  dp[0][0] = 0;
  vector<ll> a(n + 1), b(n + 1);
  rep(i, 1, n + 1) {
    cin >> a[i] >> b[i];
    rep(j, 0, k + 1) {
      dp[i][j] = dp[i - 1][j];
      if (a[i] <= j and dp[i - 1][j - a[i]] != -1) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]] + b[i]);
      }
    }
  }
  ll ans = 0;
  rep(i, 0, k + 1) ans = max(ans, dp[n][i]);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cin>>t;
  tt = t;
  while (t--) {
    solve();
    if (t)
      cout << "\n";
  }
  return 0;
}


Fixed submission:
#pragma GCC optimze("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define trace(...)                                                             \
  cout << "Line:" << __LINE__ << " ";                                          \
  __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...);
}
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (ll i = a; i < b; i += 1)
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld", &x)
ll t = 1, tt;
const ll inf = 1e18, N = 100005;
ll dp[105][N];
inline ll mult(ll x, ll y) {
  x %= hell;
  y %= hell;
  return (1LL * x * y) % hell;
}
inline ll add(ll x, ll y) {
  x += y;
  return x % hell;
}
inline ll powMod(ll base, ll expo) {
  if (base == 0)
    return 0;
  ll ans = 1;
  while (expo) {
    if (expo & 1)
      ans = mult(ans, base);
    base = mult(base, base);
    expo >>= 1;
  }
  return ans;
}
void solve() {
  memset(dp, -1, sizeof dp);
  ll n, k;
  cin >> n >> k;
  dp[0][0] = 0;
  vector<ll> a(n + 1), b(n + 1);
  rep(i, 1, n + 1) {
    cin >> a[i] >> b[i];
    rep(j, 0, k + 1) {
      dp[i][j] = dp[i - 1][j];
      if (a[i] <= j and dp[i - 1][j - a[i]] != -1) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]] + b[i]);
      }
    }
  }
  ll ans = 0;
  rep(i, 0, k + 1) ans = max(ans, dp[n][i]);
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cin>>t;
  tt = t;
  while (t--) {
    solve();
    if (t)
      cout << "\n";
  }
  return 0;
}