Problem p03376 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define rrep(i, x, y) for (int i = x; i >= y; i--)
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define per0(i, n) for (int i = (n)-1; i >= 0; i--)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll read1() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 1100;
const ll inf = 1000000000000LL;
vector<int> g[N + 5];
ll w[N + 5], v[N + 5];
int n, x, d, len;
ll dp[1000010];
vector<pair<ll, ll>> a;
void dfs(int k) {
  v[k] = 1;
  int nn = g[k].size();
  rep(i, 0, nn - 1) {
    int u = g[k][i];
    dfs(u);
    v[k] += v[u];
    w[k] += w[u];
  }
}
struct Node {
  ll w, v, num;
} b[N + 5];
bool cmp(const Node x, const Node y) { return x.v * y.w >= x.w * y.v; }
ll work() {
  ll ans = 0;
  rep(i, 0, 125000) if (dp[i] <= x) {
    ll res = i;
    ll now = x - dp[i];
    rep(j, 1, len) if (b[j].num == inf || 1.0 * b[j].num * b[j].w > 1.0 * now) {
      res += now / b[j].w * b[j].v;
      break;
    }
    else res += b[j].num * b[j].v, now -= b[j].num * b[j].w;
    ans = max(ans, res);
  }
  return ans;
}
int main() {
  n = read();
  x = read();
  d = read();
  w[1] = read1();
  rep(i, 2, n) {
    w[i] = read1();
    int x = read();
    g[x].pb(i);
  }
  dfs(1);
  rep(i, 1, n) {
    int num = min(d, 50);
    if (i == 1)
      num = 50;
    int pw = 1;
    while (num >= pw) {
      a.pb(mp(w[i] * pw, v[i] * pw));
      num -= pw;
      pw <<= 1;
    }
    if (num)
      a.pb(mp(w[i] * num, v[i] * num));
    if (i == 1 || d > 50)
      b[++len] = {w[i], v[i], d - 50LL};
    if (i == 1)
      b[len].num = inf;
  }
  sort(b + 1, b + len + 1, cmp);
  rep(i, 0, 125000) dp[i] = inf;
  dp[0] = 0;
  for (auto x : a)
    rrep(j, 125000, x.Y) dp[j] = min(dp[j], dp[j - x.Y] + x.X);
  printf("%lld\n", work());
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define il inline
#define db double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define rrep(i, x, y) for (int i = x; i >= y; i--)
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define per0(i, n) for (int i = (n)-1; i >= 0; i--)
#define ept 1e-9
#define INF 0x3f3f3f3f
#define sz(x) (x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll read1() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int N = 1100;
const ll inf = 1000000000000LL;
vector<int> g[N + 5];
ll w[N + 5], v[N + 5];
int n, x, d, len;
ll dp[1000010];
vector<pair<ll, ll>> a;
void dfs(int k) {
  v[k] = 1;
  int nn = g[k].size();
  rep(i, 0, nn - 1) {
    int u = g[k][i];
    dfs(u);
    v[k] += v[u];
    w[k] += w[u];
  }
}
struct Node {
  ll w, v, num;
} b[N + 5];
bool cmp(const Node x, const Node y) {
  return 1.0 * x.v / x.w >= 1.0 * y.v / y.w;
}
ll work() {
  ll ans = 0;
  rep(i, 0, 125000) if (dp[i] <= x) {
    ll res = i;
    ll now = x - dp[i];
    rep(j, 1, len) if (b[j].num == inf || 1.0 * b[j].num * b[j].w > 1.0 * now) {
      res += now / b[j].w * b[j].v;
      break;
    }
    else res += b[j].num * b[j].v, now -= b[j].num * b[j].w;
    ans = max(ans, res);
  }
  return ans;
}
int main() {
  n = read();
  x = read();
  d = read();
  w[1] = read1();
  rep(i, 2, n) {
    w[i] = read1();
    int x = read();
    g[x].pb(i);
  }
  dfs(1);
  rep(i, 1, n) {
    int num = min(d, 50);
    if (i == 1)
      num = 50;
    int pw = 1;
    while (num >= pw) {
      a.pb(mp(w[i] * pw, v[i] * pw));
      num -= pw;
      pw <<= 1;
    }
    if (num)
      a.pb(mp(w[i] * num, v[i] * num));
    if (i == 1 || d > 50)
      b[++len] = {w[i], v[i], d - 50LL};
    if (i == 1)
      b[len].num = inf;
  }
  sort(b + 1, b + len + 1, cmp);
  rep(i, 0, 125000) dp[i] = inf;
  dp[0] = 0;
  for (auto x : a)
    rrep(j, 125000, x.Y) dp[j] = min(dp[j], dp[j - x.Y] + x.X);
  printf("%lld\n", work());
  return 0;
}