This page is still under construction.

Parts of this page are still being built. What you see may change.

Destroying the Graph

Time limit1sMemory limit512 MB

Summary
Find the minimum cost to cover every arc of a directed graph by choosing, for each vertex, to delete its incoming or outgoing arcs.
Level

Hard8 of 10

Topics
Graph, Minimum spanning tree, Greedy, Sorting
Solved
No attempts yet

Problem

Alice and Bob play the following game. First, Alice draws a directed graph with NN vertices and MM arcs. Then Bob tries to destroy it by removing every arc. In one move Bob picks any vertex and removes either all arcs coming into that vertex or all arcs going out of that vertex.

Alice assigns two costs to each vertex ii: Wi+W_i^{+} and Wi−W_i^{-}. If Bob removes all arcs incoming into vertex ii he pays Alice Wi+W_i^{+} dollars, and if he removes all arcs outgoing from vertex ii he pays Wi−W_i^{-} dollars. Determine the minimum total amount Bob must pay to remove all arcs from the graph.

Input

The first line contains two integers NN and MM (1≤N≤1001 \le N \le 100, 1≤M≤50001 \le M \le 5000). The second line contains NN integers W1+,…,WN+W_1^{+}, \dots, W_N^{+}. The third line contains W1−,…,WN−W_1^{-}, \dots, W_N^{-} in the same way. All costs are positive integers not exceeding 10610^6. Each of the following MM lines contains two integers aa and bb, describing an arc from vertex aa to vertex bb. The graph may contain loops and parallel arcs.

Output

Print a single integer WW — the minimum total amount Bob must pay to remove all arcs from the graph.

Examples4

  1. Example 1

    Input
    3 6
    1 2 3
    4 2 1
    1 2
    1 1
    3 2
    1 2
    3 1
    2 3
    
    Expected output
    5
    
  2. Example 2

    Input
    1 1
    3
    5
    1 1
    
    Expected output
    3
    
  3. Example 3

    Input
    1 1
    5
    2
    1 1
    
    Expected output
    2
    
  4. Example 4

    Input
    2 1
    10 7
    4 9
    1 2
    
    Expected output
    4