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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

int n, k;
int h[125252], s[125252], p[125252], l[125252];
vi children[125252];

ll arr[125][125252];
ll beg[125252];

ll tmp[125252];

int di[125252];
ll dv[125252];

void lk(ll *v, ll a, ll n, ll *ret) {
  REP(i, k + 1) ret[i] = v[i];
  int s = 0, t = 0;
  REP(i, k + 1) {
    ll v = ret[i] - i * a;
    while (s < t && dv[t - 1] <= v)
      t--;
    di[t] = i;
    dv[t] = v;
    t++;
    ret[i] = dv[s] + i * a;
    if (di[s] == i - n)
      s++;
  }
}

void dfs(int p, ll *iv) {
  // use s[p], max h[p]
  lk(iv, s[p], h[p], arr[p]);
  // children
  for (int c : children[p]) {
    dfs(c, iv);
    iv = arr[c];
    ll adv = (ll)l[c] * s[p];
    lk(iv, s[p], h[p] - l[c], tmp);
    REP(i, k + 1 - l[c]) CHMAX(arr[p][i + l[c]], tmp[i] + adv);
  }
}

int main() {
  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    REP(i, n) scanf("%d", h + i);
    REP(i, n) scanf("%d", s + i);
    FOR(i, 1, n) scanf("%d", p + i), p[i]--;
    FOR(i, 1, n) scanf("%d", l + i);
    p[0] = -1;
    REP(i, n) children[i].clear();
    FOR(i, 1, n) children[p[i]].push_back(i);
    REP(i, n) sort(ALL(children[i]), [&](int a, int b) { return l[a] < l[b]; });
    REP(i, k + 1) beg[i] = -1ll << 60;
    beg[0] = 0;
    dfs(0, beg);
    printf("%lld\\n", *max_element(arr[0], arr[0] + k + 1));
    // fflush(stdout);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

int n, k;
int h[125252], s[125252], p[125252], l[125252];
vi children[125252];
ll arr[125][125252];
ll iv[125][125252];

ll tmp[125252];

void lk(ll *v, ll a, ll n, ll *ret) {
  ll c = 1ll;
  REP(i, k + 1) ret[i] = v[i];
  while (n > 0) {
    ll use = min(c, n);
    n -= use;
    ll adv = use * a;
    FORR(i, 0, k + 1 - use) CHMAX(ret[i + use], ret[i] + adv);
    c <<= 1;
  }
}

void dfs(int p) {
  // use s[p], max h[p]
  lk(iv[p], s[p], h[p], arr[p]);
  // children
  for (int c : children[p]) {
    REP(i, k + 1) iv[c][i] = iv[p][i];
    dfs(c);
    REP(i, k + 1) iv[p][i] = arr[c][i];
    // use s[p], just l[c]
    REP(i, k + 1) tmp[i] = -1ll << 60;
    ll adv = (ll)l[c] * s[p];
    FORR(i, 0, k + 1 - l[c]) tmp[i + l[c]] = iv[p][i] + adv;
    // use s[p], max h[p]-l[c]
    lk(tmp, s[p], h[p] - l[c], tmp);
    REP(i, k + 1) CHMAX(arr[p][i], tmp[i]);
  }
}

int main() {
  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    REP(i, n) scanf("%d", h + i);
    REP(i, n) scanf("%d", s + i);
    FOR(i, 1, n) scanf("%d", p + i), p[i]--;
    FOR(i, 1, n) scanf("%d", l + i);
    p[0] = -1;
    REP(i, n) children[i].clear();
    FOR(i, 1, n) children[p[i]].push_back(i);
    REP(i, n) sort(ALL(children[i]), [&](int a, int b) { return l[a] < l[b]; });
    REP(i, k + 1) iv[0][i] = -1ll << 60;
    iv[0][0] = 0;
    dfs(0);
    printf("%lld\\n", *max_element(arr[0], arr[0] + k + 1));
    // fflush(stdout);
  }
  return 0;
}