Problem p03376 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> il read(T &x) {
  int f = 1;
  char k = getchar();
  x = 0;
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = (x << 3) + (x << 1) + k - '0';
  x *= f;
}
template <class T> il _print(T x) {
  if (x / 10)
    _print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> il print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  _print(x);
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int m, int mod) {
  int res = 1, bas = x;
  while (m) {
    if (m & 1)
      res = (1ll * res * bas) % mod;
    bas = (1ll * bas * bas) % mod, m >>= 1;
  }
  return res;
}
const int N = 55;
int n, m, D, fa, lim, mx;
vector<int> G[N];
il add_edge(int u, int v) { G[u].push_back(v); }
struct Node {
  ll val, w, id;
  bool operator<(const Node &t) const { return w * t.val > t.w * val; }
} node[N];
ll ans, val[N], w[N], f[N * N];
// f[i] ±íÊ¾µÃµ½ i µÄ¼ÛÖµ£¬ÖÁÉÙÐèÒª¶àÉÙÖØÁ¿
il Insert(ll w, ll v) {
  for (ri i = mx; i >= w; --i)
    f[i] = min(f[i], f[i - w] + v);
}
il Updata(Node t) {
  int x = lim;
  for (ri i = 0; (1 << i) <= x; ++i) {
    Insert(t.w * (1 << i), t.val * (1 << i));
    x -= 1 << i;
  }
  if (x)
    Insert(t.w * x, t.val * x);
}
il DFS(int u, int fa) {
  w[u] = 1;
  for (auto v : G[u])
    DFS(v, u), val[u] += val[v], w[u] += w[v];
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n), read(m), read(D), read(val[1]);
  for (ri i = 2; i <= n; ++i)
    read(val[i]), read(fa), add_edge(fa, i);
  DFS(1, 0), lim = min(n, D), mx = n * n * n;
  for (ri i = 1; i <= n; ++i)
    node[i] = (Node){val[i], w[i], i};
  for (ri i = 1; i <= mx; ++i)
    f[i] = 1e18;
  for (ri i = 1; i <= n; ++i)
    Updata(node[i]);
  sort(node + 1, node + 1 + n);
  while (node[n].id ^ 1)
    --n;
  for (ri i = 0; i <= mx; ++i) {
    if (f[i] > m)
      continue;
    ll v = f[i], w = i;
    for (ri j = 1; j < n; ++j) {
      ll k = min(1ll * D - lim, (m - v) / node[j].val);
      v += k * node[j].val, w += k * node[j].w;
    }
    ll k = (m - v) / node[n].val;
    ans = max(ans, w + k * node[n].w);
  }
  print(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> il read(T &x) {
  int f = 1;
  char k = getchar();
  x = 0;
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = (x << 3) + (x << 1) + k - '0';
  x *= f;
}
template <class T> il _print(T x) {
  if (x / 10)
    _print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> il print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  _print(x);
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int m, int mod) {
  int res = 1, bas = x;
  while (m) {
    if (m & 1)
      res = (1ll * res * bas) % mod;
    bas = (1ll * bas * bas) % mod, m >>= 1;
  }
  return res;
}
const int N = 55;
int n, m, D, fa, lim, mx;
vector<int> G[N];
il add_edge(int u, int v) { G[u].push_back(v); }
struct Node {
  ll val, w, id;
  bool operator<(const Node &t) const { return w * t.val > t.w * val; }
} node[N];
ll ans, val[N], w[N], f[N * N * N];
// f[i] ±íÊ¾µÃµ½ i µÄ¼ÛÖµ£¬ÖÁÉÙÐèÒª¶àÉÙÖØÁ¿
il Insert(ll w, ll v) {
  for (ri i = mx; i >= w; --i)
    f[i] = min(f[i], f[i - w] + v);
}
il Updata(Node t) {
  int x = lim;
  for (ri i = 0; (1 << i) <= x; ++i) {
    Insert(t.w * (1 << i), t.val * (1 << i));
    x -= 1 << i;
  }
  if (x)
    Insert(t.w * x, t.val * x);
}
il DFS(int u, int fa) {
  w[u] = 1;
  for (auto v : G[u])
    DFS(v, u), val[u] += val[v], w[u] += w[v];
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n), read(m), read(D), read(val[1]);
  for (ri i = 2; i <= n; ++i)
    read(val[i]), read(fa), add_edge(fa, i);
  DFS(1, 0), lim = min(n, D), mx = n * n * n;
  for (ri i = 1; i <= n; ++i)
    node[i] = (Node){val[i], w[i], i};
  for (ri i = 1; i <= mx; ++i)
    f[i] = 1e18;
  for (ri i = 1; i <= n; ++i)
    Updata(node[i]);
  sort(node + 1, node + 1 + n);
  while (node[n].id ^ 1)
    --n;
  for (ri i = 0; i <= mx; ++i) {
    if (f[i] > m)
      continue;
    ll v = f[i], w = i;
    for (ri j = 1; j < n; ++j) {
      ll k = min(1ll * D - lim, (m - v) / node[j].val);
      v += k * node[j].val, w += k * node[j].w;
    }
    ll k = (m - v) / node[n].val;
    ans = max(ans, w + k * node[n].w);
  }
  print(ans);
  return 0;
}