Problem p02863 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll ac(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}
int dp[3001][3001];
int main() {
  int n, t, A, B;
  cin >> n >> t;
  vector<int> a(n), b(n);
  vector<pair<int, int>> data(n);
  rep(i, 0, n) cin >> A >> B, data[i] = {A, B};
  sort(all(data));
  rep(i, 0, n) a[i] = data[i].first, b[i] = data[i].second;
  vector<int> kouho;
  int M = 0;
  rep(i, 0, t) {
    rep(i2, 0, n) {
      if (i + a[i2] < t)
        dp[i2 + 1][i + a[i2]] = max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]);
      else {
        M = max(max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]), M);
      }
      dp[i2 + 1][i] = max(dp[i2][i], dp[i2 + 1][i]);
    }
  }
  printf("%d\n", M);
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll ac(ll n, ll k) {
  ll a = 1;
  rep(i, 1, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}
int dp[6001][6001];
int main() {
  int n, t, A, B;
  cin >> n >> t;
  vector<int> a(n), b(n);
  vector<pair<int, int>> data(n);
  rep(i, 0, n) cin >> A >> B, data[i] = {A, B};
  sort(all(data));
  rep(i, 0, n) a[i] = data[i].first, b[i] = data[i].second;
  vector<int> kouho;
  int M = 0;
  rep(i, 0, t) {
    rep(i2, 0, n) {
      if (i + a[i2] < t)
        dp[i2 + 1][i + a[i2]] = max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]);
      else {
        M = max(max(dp[i2][i] + b[i2], dp[i2 + 1][i + a[i2]]), M);
      }
      dp[i2 + 1][i] = max(dp[i2][i], dp[i2 + 1][i]);
    }
  }
  printf("%d\n", M);
  return 0;
}