Problem p03376 (C++)

Buggy submission (status: Runtime Error):
/*

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    prayer
*/

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(_, v.size()) cout << " " << v[_];                                        \
  cout << endl;
#define show2d(v)                                                              \
  rep(__, v.size()) {                                                          \
    rep(_, v[__].size()) cout << " " << v[__][_];                              \
    cout << endl;                                                              \
  }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int n, x, d;
vvi G;
vi m;
vvi in;
vi dfs(int a, int p) {
  int co = 1, sum = m[a];
  rep(i, G[a].size()) {
    int to = G[a][i];
    if (to == p)
      continue;
    vi t = dfs(to, a);
    co += t[0];
    sum += t[1];
  }
  in[a] = {co, sum, d};
  return {co, sum};
}
signed main() {
  cin >> n >> x >> d;
  G = vvi(n);
  m = vi(n);
  in = vvi(n);
  cin >> m[0];
  rep(i, n - 1) {
    int a;
    cin >> m[i + 1] >> a;
    G[a - 1].pb(i + 1);
  }
  dfs(0, -1);
  in[0][2] = inf;
  sort(all(in), [](vi a, vi b) { return a[1] * b[0] < b[1] * a[0]; });
  int ma = min(50 * 50 * 50ll, x);
  vi dp(ma + 1, inf);
  dp[0] = 0;
  rep(i, n)
      rep(j, min(50ll, in[i][2])) for (int k = ma; k >= 0; k--) if (dp[k] + inf)
          cmin(dp[k + in[i][0]], dp[k] + in[i][1]);
  int out = 0;
  rep(i, ma + 1) {
    int sum = i;
    int nokori = x - dp[i];
    if (nokori < 0)
      continue;
    rep(j, n) {
      int co = min(nokori / in[j][1], max(0ll, in[j][2] - 50));
      sum += co * in[j][0];
      nokori -= co * in[j][1];
    }
    cmax(out, sum);
  }
  cout << out << endl;
}


Fixed submission:
/*

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    prayer
*/

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(_, v.size()) cout << " " << v[_];                                        \
  cout << endl;
#define show2d(v)                                                              \
  rep(__, v.size()) {                                                          \
    rep(_, v[__].size()) cout << " " << v[__][_];                              \
    cout << endl;                                                              \
  }
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int n, x, d;
vvi G;
vi m;
vvi in;
vi dfs(int a, int p) {
  int co = 1, sum = m[a];
  rep(i, G[a].size()) {
    int to = G[a][i];
    if (to == p)
      continue;
    vi t = dfs(to, a);
    co += t[0];
    sum += t[1];
  }
  in[a] = {co, sum, d};
  return {co, sum};
}
signed main() {
  cin >> n >> x >> d;
  G = vvi(n);
  m = vi(n);
  in = vvi(n);
  cin >> m[0];
  rep(i, n - 1) {
    int a;
    cin >> m[i + 1] >> a;
    G[a - 1].pb(i + 1);
  }
  dfs(0, -1);
  in[0][2] = inf;
  sort(all(in), [](vi a, vi b) { return a[1] * b[0] < b[1] * a[0]; });
  int ma = 50 * 50 * 50ll;
  vi dp(ma + 1, inf);
  dp[0] = 0;
  rep(i, n)
      rep(j, min(50ll, in[i][2])) for (int k = ma; k >= 0; k--) if (dp[k] + inf)
          cmin(dp[k + in[i][0]], dp[k] + in[i][1]);
  int out = 0;
  rep(i, ma + 1) {
    int sum = i;
    int nokori = x - dp[i];
    if (nokori < 0)
      continue;
    rep(j, n) {
      int co = min(nokori / in[j][1], max(0ll, in[j][2] - 50));
      sum += co * in[j][0];
      nokori -= co * in[j][1];
    }
    cmax(out, sum);
  }
  cout << out << endl;
}