Problem p03526 (C++)

Buggy submission (status: Runtime Error):
/*
ABHIJEET BISWAS
CSE (2017-2021)
MNNIT ALLAHBAD
*/

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1e15
#define ll long long
#define ld long double
#define int ll
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define MAX 1000009
#define pb push_back
#define mp map<int, int>
#define F first
#define S second
#define popi __builtin_popcount
#define popll __builtin_popcountll
#define grp vector<int> graph[MAX];
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define endl "\n"

//////////////////////TEMPLATE
///ENDS///////////////////////////////////////////////////////////////////////
ll power(ll, ll);
ll mpower(ll, ll);
int BIT[MAX];
int query(int);
void update(int, int);
//////////////////////////////////////////////////////////////////////////////////////////////////////////

int n;

vector<pii> arr;
int dp[5003], tmp[5003];
int32_t main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen("../input.txt", "r", stdin);
  freopen("../output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    arr.pb({a, b});
  }

  sort(arr.begin(), arr.end(),
       [](pii a, pii b) { return a.first + a.second < b.first + b.second; });
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
  }

  for (int i = 0; i < n; i++) {
    for (int i = 0; i <= n; i++)
      tmp[i] = dp[i];
    for (int j = 0; j < n; j++) {
      if (arr[i].first >= dp[j])
        tmp[j + 1] = min(tmp[j + 1], dp[j] + arr[i].second);
    }
    for (int i = 0; i <= n; i++)
      dp[i] = tmp[i];
  }

  for (int i = n; i >= 0; i--) {
    if (dp[i] != INF) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

/*_________________________________________________Utility Functions
 * HERE_______________________________*/

void update(int x, int delta) {
  for (; x < MAX; x += x & -x)
    BIT[x] += delta;
}
int query(int x) {
  int sum = 0;
  for (; x > 0; x -= x & -x)
    sum += BIT[x];
  return sum;
}

ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a;
    a = a * a;
    b = b / 2;
  }
  return res;
}
ll mpower(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b = b / 2;
  }
  return res;
}

Fixed submission:
/*
ABHIJEET BISWAS
CSE (2017-2021)
MNNIT ALLAHBAD
*/

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1e15
#define ll long long
#define ld long double
#define int ll
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define MAX 1000009
#define pb push_back
#define mp map<int, int>
#define F first
#define S second
#define popi __builtin_popcount
#define popll __builtin_popcountll
#define grp vector<int> graph[MAX];
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define endl "\n"

//////////////////////TEMPLATE
///ENDS///////////////////////////////////////////////////////////////////////
ll power(ll, ll);
ll mpower(ll, ll);
int BIT[MAX];
int query(int);
void update(int, int);
//////////////////////////////////////////////////////////////////////////////////////////////////////////

int n;

vector<pii> arr;
int dp[5003], tmp[5003];
int32_t main() {
  fastIO

          cin >>
      n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    arr.pb({a, b});
  }

  sort(arr.begin(), arr.end(),
       [](pii a, pii b) { return a.first + a.second < b.first + b.second; });
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
  }

  for (int i = 0; i < n; i++) {
    for (int i = 0; i <= n; i++)
      tmp[i] = dp[i];
    for (int j = 0; j < n; j++) {
      if (arr[i].first >= dp[j])
        tmp[j + 1] = min(tmp[j + 1], dp[j] + arr[i].second);
    }
    for (int i = 0; i <= n; i++)
      dp[i] = tmp[i];
  }

  for (int i = n; i >= 0; i--) {
    if (dp[i] != INF) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

/*_________________________________________________Utility Functions
 * HERE_______________________________*/

void update(int x, int delta) {
  for (; x < MAX; x += x & -x)
    BIT[x] += delta;
}
int query(int x) {
  int sum = 0;
  for (; x > 0; x -= x & -x)
    sum += BIT[x];
  return sum;
}

ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a;
    a = a * a;
    b = b / 2;
  }
  return res;
}
ll mpower(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b = b / 2;
  }
  return res;
}