Problem p01603 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 310
#define MAX_T 10000

int n, T;
vector<pair<int, int>> way[SIZE];
int a[SIZE], b[SIZE], t[SIZE], m[SIZE], v[SIZE];

void dfs(vector<ll> &dp, int now, int back = -1, int eid = -1) {
  if (eid >= 0) {
    int w = max(0, m[eid] - 2) / 2;
    ll tmp_v = v[eid] * 2, tmp_t = t[eid] * 2;
    for (int j = 1; j <= w; j *= 2) {
      for (int i = T - tmp_t; i >= 0; i--) {
        dp[i + tmp_t] = max(dp[i + tmp_t], dp[i] + tmp_v);
      }
      w -= j;
      tmp_t *= 2;
      tmp_v *= 2;
    }
    tmp_v = v[eid] * 2 * w;
    tmp_t = t[eid] * 2 * w;
    for (int i = T - tmp_t; i >= 0; i--) {
      dp[i + tmp_t] = max(dp[i + tmp_t], dp[i] + tmp_v);
    }

    if (max(0, m[eid] - 2) % 2) {
      for (int i = T - t[eid] * 2; i >= 0; i--) {
        dp[i + t[eid] * 2] = max(dp[i + t[eid] * 2], dp[i] + v[eid]);
      }
    }
  }

  for (int i = 0; i < way[now].size(); i++) {
    if (way[now][i].first == back)
      continue;
    int id = way[now][i].second;

    vector<ll> dp_dfs(T + 1, -LLINF);
    for (int j = 0; j + t[id] * 2 <= T; j++)
      dp_dfs[j + t[id] * 2] = dp[j];

    dfs(dp_dfs, way[now][i].first, now, id);

    for (int j = 0; j <= T; j++)
      dp[j] = max(dp[j], dp_dfs[j] + v[id] * min(2, m[id]));
  }
}

int main() {

  scanf("%d%d", &n, &T);

  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%d%d%d", a + i, b + i, t + i, m + i, v + i);
    a[i]--;
    b[i]--;
    way[a[i]].push_back({b[i], i});
    way[b[i]].push_back({a[i], i});
  }

  vector<ll> dp(T + 1, -LLINF);

  dp[0] = 0;
  dfs(dp, 0);

  ll ans = 0;

  for (int i = 0; i <= T; i++)
    ans = max(ans, dp[i]);

  printf("%lld\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 110
#define MAX_T 10000

int n, T;
vector<pair<int, int>> way[SIZE];
int a[SIZE], b[SIZE], t[SIZE], m[SIZE], v[SIZE];

void dfs(vector<ll> &dp, int now, int back = -1, int eid = -1) {
  if (eid >= 0) {
    int w = max(0, m[eid] - 2) / 2;
    ll tmp_v = v[eid] * 2, tmp_t = t[eid] * 2;
    while (w) {
      if (w % 2) {
        for (int i = T - tmp_t; i >= 0; i--) {
          dp[i + tmp_t] = max(dp[i + tmp_t], dp[i] + tmp_v);
        }
      }
      w /= 2;
      tmp_t *= 2;
      tmp_v *= 2;
    }

    if (max(0, m[eid] - 2) % 2) {
      for (int i = T - t[eid] * 2; i >= 0; i--) {
        dp[i + t[eid] * 2] = max(dp[i + t[eid] * 2], dp[i] + v[eid]);
      }
    }
  }

  for (int i = 0; i < way[now].size(); i++) {
    if (way[now][i].first == back)
      continue;
    int id = way[now][i].second;

    vector<ll> dp_dfs(T + 1, -LLINF);
    for (int j = 0; j + t[id] * 2 <= T; j++)
      dp_dfs[j + t[id] * 2] = dp[j];

    dfs(dp_dfs, way[now][i].first, now, id);

    for (int j = 0; j <= T; j++)
      dp[j] = max(dp[j], dp_dfs[j] + v[id] * min(2, m[id]));
  }
}

int main() {

  scanf("%d%d", &n, &T);

  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%d%d%d", a + i, b + i, t + i, m + i, v + i);
    a[i]--;
    b[i]--;
    way[a[i]].push_back({b[i], i});
    way[b[i]].push_back({a[i], i});
  }

  vector<ll> dp(T + 1, -LLINF);

  dp[0] = 0;
  dfs(dp, 0);

  ll ans = 0;

  for (int i = 0; i <= T; i++)
    ans = max(ans, dp[i]);

  printf("%lld\\n", ans);

  return 0;
}