#P10166. [DTCPC 2024] 环

[DTCPC 2024] 环

Background

Cycle.

Problem Description

Given a directed graph GG with no multiple edges and no self-loops, and a sequence {an}\{a_n\}, each time you may add a directed edge iji\to j at a cost of ai+aja_i+a_j. Find the minimum total cost to make it possible to find k2k\geq 2 distinct vertices p1,p2,,pkp_1,p_2,\dots,p_k such that for all i[1,k]i\in [1,k], there is an edge pipimodk+1p_i\to p_{i\bmod k+1}.

Input Format

The first line contains two integers n,mn,m (2n5×1052\le n\le 5 \times 10^5, n1m106n-1 \le m \le 10^6), denoting the number of vertices and edges in the graph.

The second line contains nn integers, denoting the sequence {an}\{a_n\} (1ai1091\le a_i\le 10^9).

The next mm lines each contain two integers u,vu,v (1u,vn1\le u,v\le n), meaning there is a directed edge uvu\to v.

Output Format

Output one integer in a single line, denoting the minimum cost.

5 5
1 2 3 4 5 
1 2
2 3
3 4
1 5
5 4 
3

Hint

Translated by ChatGPT 5