Snake JOI

Find the shortest travel time from room 1 to room N where entering a hot room requires X minutes since last leaving a cold room, and vice versa.

Medium6Shortest pathGraphDynamic programmingNo attempts yetTime limit2sMemory limit512 MB

Problem

JOI the snake has wandered into a large mansion. He must escape before any resident of the mansion finds him.

The mansion has NN rooms, numbered 1,2,,N1, 2, \ldots, N. It also has MM corridors, and corridor ii (1iM1 \le i \le M) connects room AiA_i and room BiB_i. JOI can walk a corridor in either direction, and walking corridor ii takes DiD_i minutes. There is no way to move between rooms other than through corridors.

The temperature of each room is held constant, and each room is too cold, comfortable, or too hot for JOI. JOI cannot cope with sudden temperature changes, so he cannot enter a too-hot room less than XX minutes after he last left a too-cold room. In the same way, he cannot enter a too-cold room less than XX minutes after he last left a too-hot room.

While moving, JOI must leave a room immediately after entering it. He also cannot turn back in the middle of a corridor, and he cannot take longer than DiD_i minutes to walk corridor ii. He is allowed to enter a room he has already visited and to use a corridor he has already used.

JOI is now in room 11, which is too cold for him. When JOI enters room NN, which has the exit, he escapes from the mansion.

Find the minimum time JOI needs to escape from the mansion.

Input

The input consists of 1+N+M1 + N + M lines.

The first line contains three integers NN, MM, XX (2N100002 \le N \le 10\,000, 1M200001 \le M \le 20\,000, 1X2001 \le X \le 200) separated by spaces. The mansion has NN rooms and MM corridors, and JOI needs XX minutes to adjust to a temperature change.

The ii-th of the next NN lines (1iN1 \le i \le N) contains an integer TiT_i (0Ti20 \le T_i \le 2), the temperature of room ii. Room ii is too cold for JOI if Ti=0T_i = 0, comfortable if Ti=1T_i = 1, and too hot if Ti=2T_i = 2. It is guaranteed that T1=0T_1 = 0.

The jj-th of the next MM lines (1jM1 \le j \le M) contains three integers AjA_j, BjB_j, DjD_j (1Aj<BjN1 \le A_j < B_j \le N, 1Dj2001 \le D_j \le 200) separated by spaces. Corridor jj connects room AjA_j and room BjB_j, and walking it takes DjD_j minutes. Several corridors may connect the same pair of rooms.

The input guarantees that JOI can escape from the mansion.

Output

Print one line with one integer: the minimum number of minutes JOI needs to escape from the mansion.

Hint

In example 1, the fastest route visits the rooms in the order 123456581 \to 2 \to 3 \to 4 \to 5 \to 6 \to 5 \to 8.

In example 2, some pairs of rooms (for example, room 11 and room 55) are connected by more than one corridor.