402652: GYM100834 A Polycarp and Digits

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A. Polycarp and Digitstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Polycarp decided to get into the maze. The maze consists of N rooms connected by N - 1 corridors. In the beginning he is in the room 1, from which he can go to any other. While being in one of the rooms he tries to go to the one where he hasn't been yet. If there are no such rooms, Polycarp goes to the room where he came from or exits from the maze if he is in the room 1 and visited all other rooms. In each room there is an integer. Polycarp defined that in the room i recorded the number ai, repeated bi times. For example, if ai equal to 123 and bi equal to 3, the number will look like 123123123. Every time visiting another room, Polycarp writes the number specified number of times to his notepad. Besides it is considered that in the beginning Polycarp visits room 1. Therefore, he gets one more integer c, composed from all written numbers to the notepad. Polycarp is interested in all subsequences of the consecutive identical digits. Among these subsequences he chooses those who have the greatest length, and among the he chooses the one that is composed of the least digit. He needs to determine from what minimum digit and maximum length it is possible to construct such a subsequence.

Input

The first line contains integer N — the number of rooms. 1 ≤ N ≤ 100000. The following N lines contain pairs of integers separated by spaces: ai and bi. 1 ≤ ai ≤ 100 000, 1 ≤ bi ≤ 1000. In next N - 1 lines there are pairs of integers fi и ti — description of the corridors. Corridor i connects room fi with room ti. Polycarp checks the unvisited rooms in a sequence, written in the input file.

Output

In the first line print two numbers — the minimal digit that appears sequentially more often in c, and how many times it occurs.

ExamplesInput
3
11211 1
11111 1
11111 1
1 2
1 3
Output
1 9
Input
3
3 1
3 3
3 7
1 2
1 3
Output
3 13

加入题单

算法标签: