Problem p03376 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
#define IS_INF(x) x < 2100000000
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const int maxn = 1e6 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
ll dp[55][125005];
ll N, D, X;
ll sum[55], sz[55], fa[55], cnt, pos[55];
int main() {
#ifndef ONLINE_JUDGE
  freopen("data.in", "r", stdin);
#endif
  FAST_IO
  cin >> N >> X >> D;
  for (int i = 1; i <= N; i++) {
    cin >> sum[i];
    if (i != 1)
      cin >> fa[i];
  }
  for (int i = N; i >= 1; i--) {
    sum[fa[i]] += sum[i];
    sz[fa[i]] += ++sz[i];
    cnt += sz[i] * N;
    pos[i] = i;
  }
  sort(pos + 1, pos + 1 + N,
       [](auto x, auto y) { return sum[x] * sz[y] < sum[y] * sz[x]; });
  for (int i = 1; i <= cnt; i++)
    dp[0][i] = 1e9 + 10;
  ll tmp = min(N, D);
  for (int i = 1; i <= N; i++) {
    memcpy(dp[i], dp[i - 1], sizeof(dp[0]));
    for (int j = 0; j <= cnt; j++) {
      for (int k = 1; k <= tmp && j - k * sz[i] >= 0; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - k * sz[i]] + k * sum[i]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= cnt; i++) {
    if (dp[N][i] > X)
      continue;
    ll tmpans = i, rest = X - dp[N][i];
    for (int j = 1; j <= N; j++) {
      ll used = min(max(D - N, 0LL), rest / sum[pos[j]]);
      if (pos[j] == 1)
        used = rest / sum[pos[j]];
      rest -= sum[pos[j]] * used;
      tmpans += sz[pos[j]] * used;
    }
    ans = max(ans, tmpans);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
#define IS_INF(x) x < 2100000000
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const int maxn = 1e6 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
ll dp[55][125005];
ll N, D, X;
ll sum[55], sz[55], fa[55], cnt, pos[55];
int main() {
  FAST_IO
  cin >> N >> X >> D;
  for (int i = 1; i <= N; i++) {
    cin >> sum[i];
    if (i != 1)
      cin >> fa[i];
  }
  for (int i = N; i >= 1; i--) {
    sum[fa[i]] += sum[i];
    sz[fa[i]] += ++sz[i];
    cnt += sz[i] * N;
    pos[i] = i;
  }
  sort(pos + 1, pos + 1 + N,
       [](auto x, auto y) { return sum[x] * sz[y] < sum[y] * sz[x]; });
  for (int i = 1; i <= cnt; i++)
    dp[0][i] = 1e9 + 10;
  ll tmp = min(N, D);
  for (int i = 1; i <= N; i++) {
    memcpy(dp[i], dp[i - 1], sizeof(dp[0]));
    for (int j = 0; j <= cnt; j++) {
      for (int k = 1; k <= tmp && j - k * sz[i] >= 0; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - k * sz[i]] + k * sum[i]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= cnt; i++) {
    if (dp[N][i] > X)
      continue;
    ll tmpans = i, rest = X - dp[N][i];
    for (int j = 1; j <= N; j++) {
      ll used = min(max(D - N, 0LL), rest / sum[pos[j]]);
      if (pos[j] == 1)
        used = rest / sum[pos[j]];
      rest -= sum[pos[j]] * used;
      tmpans += sz[pos[j]] * used;
    }
    ans = max(ans, tmpans);
  }
  cout << ans << endl;
  return 0;
}