atcoder#ABC214H. [ABC214H] Collecting

[ABC214H] Collecting

Score : 600600 points

Problem Statement

There is a directed graph with NN vertices and MM edges. The vertices are numbered 1,,N1, \dots, N, and the ii-th edge (1iM)(1 \leq i \leq M) goes from Vertex AiA_i to Vertex BiB_i.

Initially, there are XiX_i items on Vertex ii (1iN)(1 \leq i \leq N) that someone has lost. KK people will collect these items.

The KK people will travel in the graph one by one. Each person will do the following.

  • Start on Vertex 11. Then, traverse an edge any finite number of times. For each vertex visited (including Vertex 11), collect all items on it if they have not been collected yet.

Find the maximum total number of items that can be collected.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1M2×1051 \leq M \leq 2 \times 10^5
  • 1K101 \leq K \leq 10
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • AiBiA_i \neq B_i
  • AiAjA_i \neq A_j or BiBjB_i \neq B_j, if iji \neq j.
  • 1Xi1091 \leq X_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM KK

A1A_1 B1B_1

\vdots

AMA_M BMB_M

X1X_1 \ldots XNX_N

Output

Print the answer.

5 5 2
1 2
2 3
3 2
1 4
1 5
1 4 5 2 8
18

The two people can collect 1818 items as follows.

  • The first person goes 12321 \rightarrow 2 \rightarrow 3 \rightarrow 2 and collects the items on Vertices 11, 22, and 33.
  • The second person goes 151 \rightarrow 5 and collects the items on Vertex 55.

It is impossible to collect 1919 or more items, so we should print 1818.

3 1 10
2 3
1 100 100
1