Timeline

Time limit2sMemory limit512 MB

Summary
Given lower bounds on each of N session dates and C constraints that one session is at least x days after another, find the earliest feasible date for every session.
Level

Medium7 of 10

Topics
Graph, Shortest path, Dynamic programming, Topological sort
Solved
No attempts yet

Problem

Bessie attended NN milking sessions (1≤N≤1051\le N\le 10^5) over the past MM days (2≤M≤1092 \le M \le 10^9). However, she is having trouble remembering when she attended each session.

For each session i=1…Ni = 1 \ldots N, she knows that it occurred no earlier than day S_iS\_i (1≤S_i≤M1\le S\_i\le M). Additionally, Bessie has CC memories (1≤C≤1051\le C\le 10^5), each described by a triple (a,b,x)(a,b,x), where she recalls that session bb happened at least xx days after aa.

Help Bessie by computing the earliest possible date of occurrence for each milking session. It is guaranteed that Bessie did not remember incorrectly; in other words, there exists an assignment of sessions to days in the range 1…M1\ldots M such that all constraints from her memories are satisfied.

Input

The first line of input contains NN, MM, and CC.

The next line contains NN space-separated integers S_1,S_2,…,S_NS\_1,S\_2,\ldots, S\_N. Each is in the range 1…M1 \ldots M.

The next CC lines contain three integers, aa, bb, and xx indicating that session bb happened at least xx days after aa. For each line, a≠ba \neq b, aa and bb are in the range 1…N1 \ldots N, and xx is in the range 1…M1 \ldots M.

Output

Output NN lines giving the earliest possible date of occurrence for each session.

Hint

Session two occurred at least five days after session one, so it cannot have occurred before day 1+5=61+5=6. Session four occurred at least two days after session two, so it cannot have occurred before day 6+2=86+2=8.

Examples1

  1. Example 1

    Input
    4 10 3
    1 2 3 4
    1 2 5
    2 4 2
    3 4 4
    
    Expected output
    1
    6
    3
    8