Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 62;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, t;
  cin >> n >> t;
  vector<pair<ll, ll>> ab;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ab.push_back({a, b});
  }

  sort(ALL(ab));

  vector<vector<ll>> dp(n, vector<ll>(t + 1, 0));
  for (int j = ab[0].first; j < t; j++) {
    dp[0][j] = ab[0].second;
  }
  dp[0][ab[0].first] = ab[0].second;
  for (int i = 1; i < n; i++) {
    ll a = ab[i].first;
    ll b = ab[i].second;
    for (int j = 0; j < t; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = a; j < t; j++) {
      chmax(dp[i][j], dp[i - 1][j - a] + b);
    }
  }

  ll ans = max(dp[n - 1][t - 1], ab[0].second);

  for (int i = 1; i < n; i++) {
    chmax(ans, dp[i - 1][t - 1] + ab[i].second);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 62;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, t;
  cin >> n >> t;
  vector<pair<ll, ll>> ab;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ab.push_back({a, b});
  }

  sort(ALL(ab));

  vector<vector<ll>> dp(n, vector<ll>(t + 1, 0));
  for (int j = ab[0].first; j < t; j++) {
    dp[0][j] = ab[0].second;
  }

  for (int i = 1; i < n; i++) {
    ll a = ab[i].first;
    ll b = ab[i].second;
    for (int j = 0; j < t; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = a; j < t; j++) {
      chmax(dp[i][j], dp[i - 1][j - a] + b);
    }
  }

  ll ans = max(dp[n - 1][t - 1], ab[0].second);

  for (int i = 1; i < n; i++) {
    chmax(ans, dp[i - 1][t - 1] + ab[i].second);
  }

  cout << ans << endl;

  return 0;
}