Under construction

Still being built. Things may change

Treasure Hunt

Time limit2sMemory limit1024 MB

Summary
Find the maximum treasure sum on a walk from room 1 in a directed graph where pulling a lever in room a adds a two-way passage between x and y.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, DFS
Solved
No attempts yet

Problem

Phoenix finally has a treasure map. Phoenix takes the map into a labyrinth full of treasure.

According to the map, the labyrinth has NN rooms and MM one-way passages, each connecting two rooms. The rooms are numbered 1 to NN, and room ii holds treasure worth bib_i. You can enter the labyrinth at room 1. With an escape kit, you can leave from any room. However, once you escape, the labyrinth collapses and you cannot go back in to look for more treasure.

Room aa has a hidden lever. When you go to room aa and pull it, a hidden two-way passage appears between rooms xx and yy.

Phoenix wants to carry out treasure whose total value is as large as possible. What is the maximum total value of treasure Phoenix can collect?

Input

The first line contains the number of rooms NN and the number of one-way passages MM, separated by a space. (1≤N≤100 0001 \le N \le 100\ 000, 1≤M≤300 0001 \le M \le 300\ 000)

The second line contains NN integers b1,b2,…,bNb_1, b_2, \ldots, b_N, separated by spaces, giving the value of the treasure in each room. (0≤bi≤1090 \le b_i \le 10^9)

From the third line onward, the next MM lines each contain two integers uiu_i and viv_i, separated by a space, giving the start room and the end room of a one-way passage. (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \neq v_i)

For any two different rooms pp and qq, there is at most one one-way passage that starts at pp and ends at qq. (1≤p,q≤N1 \le p, q \le N, p≠qp \neq q)

Line M+3M+3 contains the room aa with the lever and the two ends xx and yy of the two-way passage that appears when the lever is pulled, separated by spaces. A one-way passage from room xx to room yy, or from room yy to room xx, may already exist. (1≤a,x,y≤N1 \le a, x, y \le N, x≠yx \neq y)

Output

Print the maximum total value of treasure that can be collected in one expedition.

Examples1

  1. Example 1

    Input
    8 8
    3 2 6 7 5 2 3 2
    1 2
    2 3
    3 4
    1 5
    5 6
    6 7
    7 8
    8 6
    3 4 6
    
    Expected output
    25