Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
using ll = long long;
int mod = 1000000007;

int main(void) {
  vector<int> val_list;
  int N, T;
  cin >> N >> T;
  int A[N + 1], B[N + 1];
  A[0] = 0;
  B[0] = 0;
  rep(i, N) { cin >> A[i + 1] >> B[i + 1]; }

  pair<pair<int, int>, int> dp[N + 1][T]; // 遷移元と美味しさのpair
  rep(i, N + 1) rep(j, T) dp[i][j] = make_pair(make_pair(0, 0), 0);
  for (int i = 1; i <= N; i++) {
    int a = A[i];
    int b = B[i];
    for (int j = 0; j < T; j++) {
      if (j - a >= 0) {
        if (dp[i - 1][j].second < dp[i - 1][j - a].second + b) {
          dp[i][j] =
              make_pair(make_pair(i - 1, j - a), dp[i - 1][j - a].second + b);
        } else
          dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second);
      } else {
        dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second);
      }
    }
  }
  // cout << "reach" << endl;
  pair<int, int> nl = make_pair(0, 0);
  pair<pair<int, int>, int> op = dp[N][T - 1];
  while (op.first != nl) {
    if (dp[op.first.first][op.first.second].second != op.second) {
      val_list.push_back(op.second -
                         dp[op.first.first][op.first.second].second);
    }
    op = dp[op.first.first][op.first.second];
  }
  sort(val_list.begin(), val_list.end(), greater<int>());
  sort(B, B + N + 1, greater<int>());
  rep(i, N + 1) {
    if (val_list[i] != B[i]) {
      cout << dp[N][T - 1].second + B[i] << endl;
      return 0;
    }
  }
  cout << dp[N][T - 1].second << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
using ll = long long;
int mod = 1000000007;

int main(void) {
  vector<int> val_list;
  int N, T;
  cin >> N >> T;
  int A[N + 1], B[N + 1];
  A[0] = 0;
  B[0] = 0;
  rep(i, N) { cin >> A[i + 1] >> B[i + 1]; }

  pair<pair<int, int>, int> dp[N + 1][T]; // 遷移元と美味しさのpair
  rep(i, N + 1) rep(j, T) dp[i][j] = make_pair(make_pair(0, 0), 0);
  for (int i = 1; i <= N; i++) {
    int a = A[i];
    int b = B[i];
    for (int j = 0; j < T; j++) {
      if (j - a >= 0) {
        if (dp[i - 1][j].second < dp[i - 1][j - a].second + b) {
          dp[i][j] =
              make_pair(make_pair(i - 1, j - a), dp[i - 1][j - a].second + b);
        } else
          dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second);
      } else {
        dp[i][j] = make_pair(make_pair(i - 1, j), dp[i - 1][j].second);
      }
    }
  }
  // cout << "reach" << endl;
  pair<int, int> nl = make_pair(0, 0);
  pair<pair<int, int>, int> op = dp[N][T - 1];
  while (op.first != nl) {
    if (dp[op.first.first][op.first.second].second != op.second) {
      val_list.push_back(op.second -
                         dp[op.first.first][op.first.second].second);
    }
    op = dp[op.first.first][op.first.second];
  }
  sort(val_list.begin(), val_list.end(), greater<int>());
  sort(B, B + N + 1, greater<int>());
  rep(i, val_list.size() + 1) {
    if (i == val_list.size()) {
      cout << dp[N][T - 1].second + B[i] << endl;
      return 0;
    }
    if (val_list[i] != B[i]) {
      cout << dp[N][T - 1].second + B[i] << endl;
      return 0;
    }
  }
  cout << dp[N][T - 1].second << endl;
  return 0;
}