Cup of Tea | 프로그래밍의 벗 PivotOJ
PivotOJ

Cup of Tea

시간 제한: 1000ms메모리 제한: 2048MB출처: ICPC Asia Tehran Regional Contest 2023BOJ 33181

문제

Abolf lives in Aboland, a country consisting of nn cities and n1n - 1 two-way roads. In Aboland, one can travel from any city to any other city using these roads. Aboland’s cities are numbered from 11 to nn.

Abolbucks is a multinational chain of teahouses which serves the best tea in the world. When Abolf enters a city with an Abolbucks branch, he drinks a cup of tea and instantly reaches kk units of happiness. However, each time Abolf travels through the iith road, he must pay cic_i coins as toll which causes him to lose cic_i units of happiness.

Abolf currently resides in city 11 and wants to plan his summer trip. If at any point during his trip Abolf’s happiness drops below zero, he would stops his trip immediately. For each city tt (for 2tn2 \le t \le n), Abolf wants to know what is the minimum amount of coins he should pay to reach city tt while making sure that his happiness remains non-negative at all time, including at the destination.

He has asked you to find this amount for each city except for his home city. Note that each destination should be considered separately. Also, he may visit a city multiple times during his trip.

입력

The first line of input contains two integers nn and kk (2n31052 \le n \le 3 \cdot 10^5, 1k1091 \le k \le 10^9), the number of cities in Aboland and Abolf’s happiness after he drinks a cup of tea, respectively. Each of the next n1n - 1 lines contains three space-separated integers viv_i, uiu_i, and cic_i (1vi,uin1 \le v_i , u_i \le n, 1ci1091 \le c_i \le 10^9, uiviu_i \ne v_i) indicating that the iith road connects city uiu_i and city viv_i, and Abolf should pay cic_i coins each time he travels through this road. The last line contains nn integers a1,a2,,ana_1, a_2, \dots , a_n (0ai10 \le a_i \le 1). If ai=1a_i = 1, there is an Abolbucks branch in city ii. It is guaranteed that a1=1a_1 = 1.

출력

In the only line of the output, you should print n1n-1 integers. The iith number should be the minimum amount of coins it takes for Abolf to reach city i+1i + 1 from city 11. If there is no way to reach city i+1i + 1 such that Abolf’s happiness remains non-negative at all time, print 1-1 for that city.

예제

예제 1

입력
6 3
1 2 4
1 3 3
1 4 2
4 5 1
4 6 2
1 1 0 0 1 0
출력
-1 3 2 3 6
코드를 제출하려면 로그인하세요.