Problem p01116 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

vector<int> h, l, s;
vector<int> g[300];
vector<ll> dp[301];
vector<ll> update;

void knapsackDP(ll v, ll lim, ll clim, const vector<ll> &pre, vector<ll> &ret) {
  deque<ll> deq, deqv;
  rep(i, clim + 1) {
    ll val = pre[i] - i * v;
    while (deq.size() && deqv.back() <= val) {
      deq.pop_back();
      deqv.pop_back();
    }
    deq.push_back(i);
    deqv.push_back(val);
    ret[i] = deqv.front() + i * v;
    if (deq.front() == i - lim) {
      deq.pop_front();
      deqv.pop_front();
    }
  }
}

void dfs(int v, int clim, vector<ll> &ret) {
  knapsackDP(s[v], h[v], clim, ret, dp[v]);
  for (int nv : g[v]) {
    dfs(nv, clim - l[nv], ret);
    ret = dp[nv];
    knapsackDP(s[v], h[v] - l[nv], clim, ret, update);
    rep(i, clim + 1 - l[nv])
        maxch(dp[v][i + l[nv]], update[i] + (ll)l[nv] * s[v]);
  }
}

int main() {
  while (1) {
    int N, C;
    scanf("%d%d", &N, &C);
    if (N == 0)
      break;
    rep(i, N) {
      g[i].clear();
      dp[i].resize(C + 1, 0);
    }
    h.resize(N, 0);
    l.resize(N, 0);
    s.resize(N, 0);
    update.resize(C + 1, 0);
    rep(i, N) scanf("%d", &h[i]);
    rep(i, N) scanf("%d", &s[i]);
    rep(i, N - 1) {
      int p;
      scanf("%d", &p);
      p--;
      g[p].push_back(i + 1);
    }
    rep(i, N - 1) scanf("%d", &l[i + 1]);
    rep(i, N) {
      sort(all(g[i]), [&](const int &a, const int &b) { return l[a] < l[b]; });
    }
    vector<ll> ret(C + 1, 0);
    dfs(0, C, ret);
    cout << dp[0][C] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e18
#define mod 1000000007

vector<ll> h, l, s;
vector<ll> g[300];
ll C;

vector<ll> knapsackDP(ll v, ll lim, const vector<ll> &dp) {
  vector<ll> res(C + 1, -INF);
  deque<ll> deq, deqv;
  rep(i, C + 1) {
    ll val = dp[i] - i * v;
    while (deq.size() && deqv.back() <= val) {
      deq.pop_back();
      deqv.pop_back();
    }
    deq.push_back(i);
    deqv.push_back(val);
    res[i] = deqv.front() + i * v;
    if (deq.front() == i - lim) {
      deq.pop_front();
      deqv.pop_front();
    }
  }
  return res;
}

vector<ll> dfs(ll v, const vector<ll> &pretable) {
  vector<ll> crttable = pretable;
  vector<ll> res = knapsackDP(s[v], h[v], pretable);
  for (ll nv : g[v]) {
    crttable = dfs(nv, crttable);
    vector<ll> update = knapsackDP(s[v], h[v] - l[nv], crttable);
    rep(i, C + 1 - l[nv]) maxch(res[i + l[nv]], update[i] + l[nv] * s[v]);
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    ll N;
    cin >> N >> C;
    if (N == 0)
      break;
    rep(i, N) g[i].clear();
    h.resize(N);
    l.resize(N);
    s.resize(N);
    rep(i, N) cin >> h[i];
    rep(i, N) cin >> s[i];
    rep(i, N - 1) {
      ll p;
      cin >> p;
      p--;
      g[p].push_back(i + 1);
    }
    rep(i, N - 1) cin >> l[i + 1];
    vector<ll> initable(C + 1, -INF);
    initable[0] = 0;
    vector<ll> res = dfs(0, initable);
    ll ans = 0;
    rep(i, C + 1) maxch(ans, res[i]);
    cout << ans << endl;
  }

  return 0;
}