Travel Tickets

4.7

6 votes
Loops, Hard, Tree
Problem

In HackerLand, there are N provinces connected by N1 roads. All provinces are connected.

For every visit of province i each tourist has to pay Ai travel tickets.

People in HackerLand are kind: they want to give the tourists some free travel tickets. More specifically, at the road i, each tourist will receive ci free tickets for his or her first visit there.

M tourists are planning to travel in HackerLand. The i-th tourist wants to start his journey from the province ui and finish in the province vi. Every tourist from the province x can go only to provinces which are directly connected by road with the province x. They also do not have much time; therefore, they will visit as few provinces as possible during their journey.

Find the minimum number of travel tickets each of the tourists needs to buy beforehand.

Input
The first line contains two space-separated integers N and M.
The second line contains N space-separated integers, the i-th of which is Ai.
The i-th of the following N1 lines contains three space-separated integers ai, bi, and ci, meaning that there is a road directly connecting provinces ai and bi that gives ci free travel tickets for each tourist's first visit.
The i-th of the following M lines contains two integers ui and vi, denoting the plan of the i-th tourist to travel to province vi starting from province ui.


Output
Print M lines, the i-th of which contains one integer that denotes the number of travel tickets the i-th tourist needs.

Constraints
1N,M300,000
0Ai,ci300,000
1ai,bi,ui,viN
It is guaranteed that the road network forms a tree.

Sample Input
7 5
2 1 1 2 1 1 0
1 2 0
1 3 1
1 4 0
2 5 2
2 6 0
5 7 1
1 2
2 3
1 3
1 7
7 1
Sample Output
3
3
2
3
1
Time Limit: 2.5
Memory Limit: 256
Source Limit:
Explanation

Consider the first tourist, who wants to travel from province u1=1 to province v1=2. He pays 2 travel tickets for entering province 1, then travels along the road (1,2), earns 0 free travel tickets, and needs 1 travel ticket to pay for entry to province 2. Therefore, he needs to buy 2+1=3 travel ticket beforehand.

Consider the second tourist, who wants to travel from province u2=2 to province v2=3. He pays 1 travel ticket for entering province 2, then travels along the road (1,2), gets 0 free travel tickets, and has to pay 2 travel tickets upon entry to province 1. Then, he travels along the road (1,3), earns 1 free travel ticket, and pays that travel ticket for entry to province 3. Thus, he needs to buy 1+2=3 travel tickets in advance.

Editor Image

?