Under construction

Still being built. Things may change

Operating a Travel Agency

Time limit4sMemory limit1024 MB

Summary
Given a weighted tree where each city has a bus range, find for each city the spread of enjoyment values among all cities reachable from it by chaining buses.
Level

Medium7 of 10

Topics
Tree, Graph, Shortest path, Divide and conquer
Solved
No attempts yet

Problem

You are the CEO of a successful travel agency. You want to expand the agency into a new country, and to do so you must choose the first base city.

The country has NN cities and N−1N-1 two-way roads between them. Starting from any city, you can reach every other city by following the roads.

Each city ii has a value cic_i that represents how enjoyable the city is. The larger the value, the more enjoyable the city.

The country has an unusual restriction. The tour buses that leave city ii can only be boarded in city ii, and they can only travel to cities within distance did_i of city ii.

A tour bus from city ii can be boarded at any time as long as you are in city ii. So you can board passengers at the base city, travel to another city, and then board that city's tour bus to go farther. Switching buses many times lets you reach far more cities than using only the buses from the base city.

When choosing a base city, what matters is the difference between the maximum and minimum enjoyment values among the cities reachable from it.

Given the cities and roads, find for every city, when it is chosen as the base city, the difference between the maximum and minimum enjoyment values of the cities reachable from it.

Input

The first line contains the number of cities NN. (1≤N≤80001 \le N \le 8000)

The second line contains the enjoyment values cic_i of the cities, NN integers separated by spaces. (0≤ci≤10180 \le c_i \le 10^{18})

The third line contains the distances did_i, NN integers separated by spaces, where did_i is the distance the tour bus leaving city ii can travel. (0≤di≤10180 \le d_i \le 10^{18})

From the fourth line onward, there are N−1N-1 lines. Each line contains uiu_i, viv_i, and wiw_i separated by spaces, meaning a road of length wiw_i connects cities uiu_i and viv_i. (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \neq v_i, 1≤wi≤1091 \le w_i \le 10^9)

All given numbers are integers.

Output

For each city ii, print on the ii-th line the difference between the maximum and minimum enjoyment values of the cities reachable from city ii when it is the base city. Print NN lines in total.

Examples2

  1. Example 1

    Input
    5
    100 200 0 50 250
    8 52 100 116 23
    1 5 55
    2 5 45
    3 4 11
    1 4 15
    
    Expected output
    0
    50
    250
    250
    0
    
  2. Example 2

    Input
    7
    10 20 30 40 50 0 70
    1 2 3 4 5 6 7
    1 2 2
    1 3 3
    2 4 4
    2 5 5
    3 6 6
    3 7 7
    
    Expected output
    0
    10
    20
    30
    40
    30
    60