Problem p03183 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
typedef long long ll;
typedef long double ld;
#define pii pair<ll, ll>
#define pic pair<ll, char>
#define vi vector<ll>
#define vvi vector<vi>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define st set<ll, greater<ll>>
#define mst multiset<ll, greater<ll>>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mapp unordered_map<ll, ll>
#define ppii pair<pii, pii>
#define test(t)                                                                \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define inarr(n)                                                               \
  ll arr[n];                                                                   \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> arr[i];
#define ingra(m)                                                               \
  vvi arr(100005);                                                             \
  for (ll i = 0; i < m; i++) {                                                 \
    ll u, v;                                                                   \
    cin >> u >> v;                                                             \
    arr[u].pb(v);                                                              \
    arr[v].pb(u);                                                              \
  }
#define mem(a, b) memset(a, b, sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(), arr.end()
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y = y / 2, x = (x * x) % mod;
  }
  return res % mod;
}
pair<ll, pair<ll, ll>> arr[1000];
ll dp[1000][10001];
ll n;
bool cmp(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b) {
  if (a.fi + a.sec.fi < b.fi + b.sec.fi)
    return true;
  return false;
}
ll func(ll index, ll done) {
  if (index == n)
    return 0;
  ll &ret = dp[index][done];
  if (ret != -1)
    return ret;
  ret = 0;
  if (done <= arr[index].fi)
    ret = arr[index].sec.sec + func(index + 1, done + arr[index].sec.fi);
  ret = max(ret, func(index + 1, done));
  return ret;
}
signed main() {
  fast;
  mem(dp, -1);
  cin >> n;
  ll i;
  for (i = 0; i < n; i++)
    cin >> arr[i].sec.fi >> arr[i].fi >> arr[i].sec.sec;
  sort(arr, arr + n, cmp);
  cout << func(0, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
typedef long long ll;
typedef long double ld;
#define pii pair<ll, ll>
#define pic pair<ll, char>
#define vi vector<ll>
#define vvi vector<vi>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define st set<ll, greater<ll>>
#define mst multiset<ll, greater<ll>>
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mapp unordered_map<ll, ll>
#define ppii pair<pii, pii>
#define test(t)                                                                \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define inarr(n)                                                               \
  ll arr[n];                                                                   \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> arr[i];
#define ingra(m)                                                               \
  vvi arr(100005);                                                             \
  for (ll i = 0; i < m; i++) {                                                 \
    ll u, v;                                                                   \
    cin >> u >> v;                                                             \
    arr[u].pb(v);                                                              \
    arr[v].pb(u);                                                              \
  }
#define mem(a, b) memset(a, b, sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(), arr.end()
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y = y / 2, x = (x * x) % mod;
  }
  return res % mod;
}
pair<ll, pair<ll, ll>> arr[1000];
ll dp[1000][10001];
ll n;
bool cmp(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b) {
  if (a.fi + a.sec.fi < b.fi + b.sec.fi)
    return true;
  return false;
}
ll func(ll index, ll done) {
  if (done > 1e4)
    return 0;
  if (index == n)
    return 0;
  ll &ret = dp[index][done];
  if (ret != -1)
    return ret;
  ret = 0;
  if (done <= arr[index].fi)
    ret = arr[index].sec.sec + func(index + 1, done + arr[index].sec.fi);
  ret = max(ret, func(index + 1, done));
  return ret;
}
signed main() {
  fast;
  mem(dp, -1);
  cin >> n;
  ll i;
  for (i = 0; i < n; i++)
    cin >> arr[i].sec.fi >> arr[i].fi >> arr[i].sec.sec;
  sort(arr, arr + n, cmp);
  cout << func(0, 0);
}