#LCA0000079. Bài đố về cây (Tree Quiz)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài đố về cây (Tree Quiz)
Tree Quiz
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A rooted tree of vertices is specified by parent indices; the root has parent . For every ordered pair with , compute All values, including any duplicates, are sorted in non-decreasing order. Each query requests the -th value, with 1-based indexing. Be careful: the numerical expression may overflow 32-bit integers.
Input
First line: . Next line: parent indices (exactly one is for the root; others specify the parent). Then lines each containing an integer .
Output
Print the selected sorted sequence value for each query.
Subtasks
General constraints: ; ; các cha tạo một cây; ; giá trị cần kiểu số nguyên 64 bit.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 3
3 0 2 2 3
1
18
25
Output
0
82
124
Explanation
The minimum occurs for and equals . The maximum occurs for and equals . Rank has value .