Problem p03376 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define int ll
#define PI pair<int, int>
#define poly vector<int>
#define For(i, l, r) for (int i = (int)(l); i <= (int)(r); i++)
#define Rep(i, r, l) for (int i = (int)(r); i >= (int)(l); i--)
#define pb push_back
#define mem(a) memset((a), 0, sizeof(a))
#define fi first
#define se second
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
const int N = 52, inf = 1e9;
ll m[N];
int dp[N * N * N], jj[N * N * N], p[N], sz[N], sum, q[N];
bool cmp(int a, int b) { return sz[a] * m[b] > sz[b] * m[a]; }
signed main() {
  int n = read();
  ll x = read();
  int d = read();
  m[1] = read();
  For(i, 2, n) {
    m[i] = read();
    p[i] = read();
  }
  Rep(i, n, 1) {
    m[p[i]] += m[i];
    sz[p[i]] += ++sz[i];
  }
  For(i, 1, N * N * N - 1) dp[i] = x + 1;
  For(i, 1, n) {
    int t = i > 1 ? min(d, n) : n;
    sum += sz[i] * t;
    For(j, 0, sz[i] - 1) {
      int l = 1, r = 0;
      for (int k = j; k <= sum; k += sz[i]) {
        if (q[l] + t * sz[i] < k)
          l++;
        // cout<<k<<" "<<dp[k]<<" "<<sz[i]<<" "<<m[i]<<" "<<l<<" "<<r<<"
        // "<<dp[q[r]]<<" "<<dp[k]<<endl;
        while (l <= r && dp[q[r]] > dp[k] - (k - q[r]) / sz[i] * m[i])
          r--;
        q[++r] = k;
        jj[k] = dp[q[l]] + (k - q[l]) / sz[i] * m[i];
      }
      for (int k = j; k <= sum; k += sz[i])
        dp[k] = jj[k];
    }
  }
  For(i, 1, n) q[i] = i;
  sort(q + 1, q + n + 1, cmp);
  while (dp[sum] > x)
    sum--;
  int dq = 0, ans = sum;
  For(i, 1, n) {
    int jb = q[i] == 1 ? inf : d - min(d, n);
    while (jb && sum >= 0) {
      int jj = min((ll)jb, (x - dp[sum]) / m[q[i]]);
      dq += jj * sz[q[i]];
      x -= jj * m[q[i]];
      jb -= jj;
      // out<<dq<<" "<<sum<<" "<<dp[sum]<<" "<<x<<" "<<jj<<" "<<q[i]<<"
      // "<<m[q[i]]<<endl;
      ans = max(ans, dq + sum);
      if (jb)
        sum--;
    }
  }
  // For(i,0,sum)wri(dp[i]); puts(""); cout<<q[1]<<endl;
  //	cout<<(1000000000-1000000*m[3])/m[1]*sz[1]+3000000<<"
  //"<<(1000000000-1000000*m[3])%m[1]<<" "<<dp[41]<<endl;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define int ll
#define PI pair<int, int>
#define poly vector<int>
#define For(i, l, r) for (int i = (int)(l); i <= (int)(r); i++)
#define Rep(i, r, l) for (int i = (int)(r); i >= (int)(l); i--)
#define pb push_back
#define mem(a) memset((a), 0, sizeof(a))
#define fi first
#define se second
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
const int N = 52, inf = 1e9;
ll m[N];
int dp[N * N * N], jj[N * N * N], p[N], sz[N], sum, q[N * N * N];
bool cmp(int a, int b) { return sz[a] * m[b] > sz[b] * m[a]; }
signed main() {
  int n = read();
  ll x = read();
  int d = read();
  m[1] = read();
  For(i, 2, n) {
    m[i] = read();
    p[i] = read();
  }
  Rep(i, n, 1) {
    m[p[i]] += m[i];
    sz[p[i]] += ++sz[i];
  }
  For(i, 1, N * N * N - 1) dp[i] = x + 1;
  For(i, 1, n) {
    int t = i > 1 ? min(d, n) : n;
    sum += sz[i] * t;
    For(j, 0, sz[i] - 1) {
      int l = 1, r = 0;
      for (int k = j; k <= sum; k += sz[i]) {
        if (q[l] + t * sz[i] < k)
          l++;
        // cout<<k<<" "<<dp[k]<<" "<<sz[i]<<" "<<m[i]<<" "<<l<<" "<<r<<"
        // "<<dp[q[r]]<<" "<<dp[k]<<endl;
        while (l <= r && dp[q[r]] > dp[k] - (k - q[r]) / sz[i] * m[i])
          r--;
        q[++r] = k;
        jj[k] = dp[q[l]] + (k - q[l]) / sz[i] * m[i];
      }
      for (int k = j; k <= sum; k += sz[i])
        dp[k] = jj[k];
    }
  }
  For(i, 1, n) q[i] = i;
  sort(q + 1, q + n + 1, cmp);
  while (dp[sum] > x)
    sum--;
  int dq = 0, ans = sum;
  For(i, 1, n) {
    int jb = q[i] == 1 ? inf : d - min(d, n);
    while (jb && sum >= 0) {
      int jj = min((ll)jb, (x - dp[sum]) / m[q[i]]);
      dq += jj * sz[q[i]];
      x -= jj * m[q[i]];
      jb -= jj;
      // out<<dq<<" "<<sum<<" "<<dp[sum]<<" "<<x<<" "<<jj<<" "<<q[i]<<"
      // "<<m[q[i]]<<endl;
      ans = max(ans, dq + sum);
      if (jb)
        sum--;
    }
  }
  // For(i,0,sum)wri(dp[i]); puts(""); cout<<q[1]<<endl;
  //	cout<<(1000000000-1000000*m[3])/m[1]*sz[1]+3000000<<"
  //"<<(1000000000-1000000*m[3])%m[1]<<" "<<dp[41]<<endl;
  cout << ans << endl;
}