## CEOI '19 P5 - Magic Tree

View as PDF

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

We have a magic tree: a rooted tree on vertices. The vertices are numbered through . Vertex is the root.

The magic tree gives us magic fruit. The fruit only grows in vertices of the tree other than the root. Each vertex contains at most one piece of fruit.

It is now day and no fruit is ripe yet. Each fruit will only be ripe for a single day. For each fruit, we are given the vertex where it grows, the day on which it will be ripe, and the amount of magic juice we can extract from it if we harvest it when it is ripe.

The fruits have to be harvested by cutting some branches of the tree. On each day, you may cut as many branches of the tree as you like. The parts of the tree you cut off will fall to the ground and you can collect all the ripe fruits they contain. All fruits that fall to the ground when they are not ripe are discarded and no magic juice is collected from them.

Formally, on each day, you may erase some edges of the tree. Whenever you do so, the tree will split into multiple connected components. You then erase all components that do not contain the root and you harvest all ripe fruits those components contained.

Given is a description of the tree together with the locations, ripening days and juiciness of all fruits. Calculate the maximum total amount of magic juice we can harvest from the tree.

#### Input

The first line contains three space-separated integers , and – the number of vertices, the number of fruits, and the maximum day on which a fruit may become ripe.

The following lines contain the integers , one per line. For each (from to , inclusive), vertex is the parent of vertex .

Each of the last lines describes one fruit. The -th of these lines has the form .

It is guaranteed that no vertex contains more than one fruit (i.e., the values are distinct).

#### Output

Output a single line with a single integer, the maximum amount of magic juice we can harvest from the tree.

#### Scoring

Subtask ( points): , and for all

Subtask ( points): fruits only grow in the leaves of the tree

Subtask ( points): for each , and for all

Subtask ( points): , and for all

#### Sample Input

6 4 10
1
2
1
4
4
3 4 5
4 7 2
5 4 1
6 9 3

#### Sample Output

9

#### Note

In the example input, one optimal solution looks as follows:

• On day , cut the edge between vertices and and harvest a ripe fruit with unit of magic juice. On the same day, cut the edge between vertices and and harvest units of magic juice from the ripe fruit in vertex .
• On day , do nothing. (We could harvest the fruit in vertex that just became ripe, but doing so is not optimal.)
• On day , cut the edge between vertices and . Discard the fruit in vertex that is no longer ripe, and harvest units of magic juice from the ripe fruit in vertex . (Alternately, we could achieve the same effect by cutting the edge between vertices and .)