QOJ.ac

QOJ

时间限制: 1.5 s 内存限制: 512 MB 总分: 100

#14453. Network Renovation

统计

Xiao C has a directed graph with $n$ vertices and $m$ edges, where the vertices are numbered $1, 2, \dots, n$, and the $i$-th edge is $u_i \to v_i$.

Xiao C wants to modify this directed graph such that the resulting new graph contains no cycles.

For the $i$-th edge, Xiao C can pay a cost of $a_i$ to reverse it (i.e., change it to $v_i \to u_i$), or pay a cost of $b_i$ to delete it.

For the $i$-th vertex, Xiao C can pay a cost of $c_i$ to delete it along with all edges connected to it.

Please write a program to find the modification plan with the minimum total cost.

Input

The first line contains two positive integers $n, m$ ($2 \le n \le 22$, $1 \le m \le n(n - 1)$), representing the number of vertices and edges, respectively.

The second line contains $n$ positive integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^6$), representing the cost to delete each vertex.

The next $m$ lines each contain four positive integers $u_i, v_i, a_i, b_i$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le a_i, b_i \le 10^6$), describing each edge.

The input data guarantees that there is at most one edge of each orientation between any pair of vertices.

Output

Output a single integer, which is the minimum total cost.

Examples

Input 1

4 4
9 5 6 8
1 2 5 8
2 3 7 6
3 4 9 8
4 1 4 7

Output 1

4

Input 2

4 6
3 9 9 9
1 2 7 8
2 1 4 5
1 3 7 6
3 1 5 5
1 4 8 9
4 1 9 7

Output 2

3

Input 3

3 1
1 1 1
1 2 3 4

Output 3

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.