Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 2,849 Bytes
d3f4f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

const int LIM = 1000001;

int N;
long long K;
int C[LIM];
vector<int> ch[LIM];

// <sum, negative # of paths>
pair<long long, int> DP[LIM][3];            // [node][# of incoming paths]
pair<long long, int> DP2[3][2], nxt[3][2];  // [# of ongoing paths][counted node?]

void rec(int i, int p, long long P) {
  for (auto c : ch[i]) {
    rec(c, i, P);
  }
  memset(DP2, 0, sizeof DP2);
  DP2[1][1] = make_pair(C[i], 0);
  memcpy(nxt, DP2, sizeof nxt);
  for (auto c : ch[i]) {
    for (int j = 2; j >= 0; j--) {
      for (int b = 1; b >= 0; b--) {
        auto d = DP2[j][b];
        for (int j2 = 0; j2 <= 2; j2++) {
          auto d2 = DP[c][j2];
          for (int u = 0; u <= j2; u++) {
            int r = j2 - u;
            if (r > j) {
              continue;
            }
            int j3 = j + u - r;
            if (j3 < 0 || j3 > 2) {
              continue;
            }
            int b2 = b | (j2 ? 1 : 0);
            long long dv = d.first + d2.first + (!b && b2 ? C[i] : 0) - r * P;
            int dk = d.second + d2.second - r;
            nxt[j3][b2] = max(nxt[j3][b2], make_pair(dv, dk));
          }
        }
      }
    }
    memcpy(DP2, nxt, sizeof nxt);
  }
  for (int j = 0; j <= 2; j++) {
    for (int j2 = 0; j2 <= j; j2++) {
      int e = j - j2;
      for (int b : {0, 1}) {
        auto d = DP2[j][b];
        DP[i][j2] = max(DP[i][j2], make_pair(d.first - e * P, d.second - e));
      }
    }
  }
}

pair<long long, int> solve(long long P) {
  memset(DP, 0, sizeof DP);
  rec(0, -1, P);
  auto d = DP[0][0];
  return make_pair(d.first + -d.second * P, -d.second - 1);
}

int solve() {
  for (int i = 0; i < LIM; i++) {
    ch[i].clear();
  }
  // Input.
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  for (int i = 1, j; i <= N - 1; i++) {
    cin >> j;
    ch[j - 1].push_back(i);
  }
  // Binary search for appropriate path penalty.
  long long r1 = -1, r2 = (long long)N * 1e9 + 1;
  while (r2 > r1) {
    long long m = (r1 + r2 + 1) >> 1;
    auto ans = solve(m);
    if (ans.first < K) {
      r2 = m - 1;
    } else {
      r1 = m;
    }
  }
  // Compute final answer.
  int ans = -1;
  if (r1 >= 0) {
    pair<long long, int> ans1 = solve(r1), ans2 = solve(r1 + 1);
    assert(ans2.first < K && K <= ans1.first && ans2.second < ans1.second);
    long long diffV = ans1.first - ans2.first;
    int diffK = ans1.second - ans2.second;
    long long need = K - ans2.first;
    ans = ans2.second + (need * diffK + diffV - 1) / diffV;
    assert(ans2.second < ans && ans <= ans1.second);
  }
  return ans;
}

int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cout << "Case #" << t << ": " << solve() << endl;
  }
  return 0;
}