Problem p03183 (C++)

Buggy submission (status: Runtime Error):
/*
___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr
andarash mazide nemat.
___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat.
___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb.

___az dast o zabane ke barayad ?
___kaz ohdeye shokrash be dar ayad !


___man be to dami gharar natvanam kard
___va ehsane to ra shomar natvanam kard
####################
___gar bar tane man zaban shavad har moyee
___yek shokre to az hezar natvanam kard

*/

#include <bits/stdc++.h>

#define pb push_back
#define SALI main
#define left(x) ((x)*2)
#define right(x) (((x)*2) + 1)
#define fuck(x) cout << #x << " : " << x << endl;
#define findLowVec(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define findUpVec(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define findLowArr(a, n, x) (lower_bound(a, a + n, x) - a)
#define findUpArr(a, n, x) (upper_bound(a, a + n, x) - a)
#define int long long

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &v) {
  out << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      out << ", ";
    out << v[i];
  }
  return out << "]";
}

template <class A, class B>
ostream &operator<<(ostream &out, const set<A, B> &s) {
  out << "[";
  for (auto i = s.begin(); i != s.end(); i++) {
    out << ", ";
    out << *i;
  }
  return out << "]";
}

template <class A> ostream &operator<<(ostream &out, const set<A> &s) {
  out << "[";
  for (auto i = s.begin(); i != s.end(); i++) {
    out << ", ";
    out << *i;
  }
  return out << "]";
}

const int N = 1e4 + 10;
int dp[N];

struct stone {
  int s;
  int w;
  int v;
};

bool cmp(stone a, stone b) { return a.s + a.w < b.s + b.w; }

int32_t SALI() {
  int n;
  cin >> n;
  stone a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i].w >> a[i].s >> a[i].v;
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  sort(a, a + n, cmp);
  for (int i = 0; i < n; i++) {
    for (int j = N - 1; j >= 0; j--) {
      if (dp[j] < 0)
        continue;
      if (j <= a[i].s) {
        dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i]);
  cout << ans;
}

/**< WRITEN BY ALI ADELKHAH */


Fixed submission:
/*
___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr
andarash mazide nemat.
___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat.
___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb.

___az dast o zabane ke barayad ?
___kaz ohdeye shokrash be dar ayad !


___man be to dami gharar natvanam kard
___va ehsane to ra shomar natvanam kard
####################
___gar bar tane man zaban shavad har moyee
___yek shokre to az hezar natvanam kard

*/

#include <bits/stdc++.h>

#define pb push_back
#define SALI main
#define left(x) ((x)*2)
#define right(x) (((x)*2) + 1)
#define fuck(x) cout << #x << " : " << x << endl;
#define findLowVec(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define findUpVec(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define findLowArr(a, n, x) (lower_bound(a, a + n, x) - a)
#define findUpArr(a, n, x) (upper_bound(a, a + n, x) - a)
#define int long long

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &v) {
  out << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      out << ", ";
    out << v[i];
  }
  return out << "]";
}

template <class A, class B>
ostream &operator<<(ostream &out, const set<A, B> &s) {
  out << "[";
  for (auto i = s.begin(); i != s.end(); i++) {
    out << ", ";
    out << *i;
  }
  return out << "]";
}

template <class A> ostream &operator<<(ostream &out, const set<A> &s) {
  out << "[";
  for (auto i = s.begin(); i != s.end(); i++) {
    out << ", ";
    out << *i;
  }
  return out << "]";
}

const int N = 2e4 + 10;
int dp[N];

struct stone {
  int s;
  int w;
  int v;
};

bool cmp(stone a, stone b) { return a.s + a.w < b.s + b.w; }

int32_t SALI() {
  int n;
  cin >> n;
  stone a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i].w >> a[i].s >> a[i].v;
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  sort(a, a + n, cmp);
  for (int i = 0; i < n; i++) {
    for (int j = N - 1; j >= 0; j--) {
      if (dp[j] < 0)
        continue;
      if (j <= a[i].s) {
        dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, dp[i]);
  cout << ans;
}

/**< WRITEN BY ALI ADELKHAH */