5/22/24, 12:09 PM Problem - 1746D - Codeforces
|
stdfloat | Logout
HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP
PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST
Codeforces Global Round 23
D. Paths on the Tree Finished
time limit per test: 3 seconds Practice
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given a rooted tree consisting of n vertices. The vertices are numbered from 1 to n , → Virtual participation
and the root is the vertex 1 . You are also given a score array s1 , s2 , … , sn .
→ Clone Contest to Mashup
A multiset of k simple paths is called valid if the following two conditions are both true.
Each path starts from 1 .
→ Submit?
Let ci be the number of paths covering vertex i . For each pair of vertices (u, v) (
2 ≤ u, v ≤ n) that have the same parent, |cu − cv | ≤ 1 holds.
Language: GNU G++20 13.2 (64 bit, winlibs)
n
The value of the path multiset is defined as ∑ ci si . Choose
file:
Choose File No file chosen
i=1
It can be shown that it is always possible to find at least one valid multiset. Find the maximum Submit
value among all valid multisets.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 104 ) → Contest materials
— the number of test cases. The description of the test cases follows.
Announcement (en)
The first line of each test case contains two space-separated integers n (2 ≤ n ≤ 2 ⋅ 105 ) Tutorial (en)
and k (1 ≤ k ≤ 109 ) — the size of the tree and the required number of paths.
The second line contains n − 1 space-separated integers p 2 , p 3 , … , p n (1 ≤ p i ≤ n ),
where p i is the parent of the i -th vertex. It is guaranteed that this value describe a valid tree
with root 1 .
The third line contains n space-separated integers s1 , s2 , … , sn (0 ≤ si ≤ 104 ) — the
scores of the vertices.
5
It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10 .
Output
For each test case, print a single integer — the maximum value of a path multiset.
Example
input Copy
2
5 4
1 2 1 3
6 2 1 5 7
5 3
1 2 1 3
6 6 1 4 10
output Copy
54
56
Note
https://mirror.codeforces.com/problemset/problem/1746/D 1/2
5/22/24, 12:09 PM Problem - 1746D - Codeforces
In the first test case, one of optimal solutions is four paths 1 → 2 → 3 → 5 ,
1 → 2 → 3 → 5 , 1 → 4, 1 → 4, here c = [4, 2, 2, 2, 2]. The value equals to
4 ⋅ 6 + 2 ⋅ 2 + 2 ⋅ 1 + 2 ⋅ 5 + 2 ⋅ 7 = 54 .
In the second test case, one of optimal solution is three paths 1 → 2 → 3 → 5,
1 → 2 → 3 → 5 , 1 → 4, here c = [3, 2, 2, 1, 2]. The value equals to
3 ⋅ 6 + 2 ⋅ 6 + 2 ⋅ 1 + 1 ⋅ 4 + 2 ⋅ 10 = 56.
Codeforces (c) Copyright 2010-2024 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: May/22/2024 12:06:39UTC+5 (l1).
Desktop version, switch to mobile version.
Privacy Policy
Supported by
https://mirror.codeforces.com/problemset/problem/1746/D 2/2