atcoder#ARC107F. [ARC107F] Sum of Abs

[ARC107F] Sum of Abs

Score : 900900 points

Problem Statement

Given is a simple undirected graph with NN vertices and MM edges. Its vertices are numbered 1,2,,N1, 2, \ldots, N and its edges are numbered 1,2,,M1, 2, \ldots, M. On Vertex ii (1iN1 \leq i \leq N) two integers AiA_i and BiB_i are written. Edge ii (1iM1 \leq i \leq M) connects Vertices UiU_i and ViV_i.

Snuke picks zero or more vertices and delete them. Deleting Vertex ii costs AiA_i. When a vertex is deleted, edges that are incident to the vertex are also deleted. The score after deleting vertices is calculated as follows:

  • The score is the sum of the scores of all connected components.
  • The score of a connected component is the absolute value of the sum of BiB_i of the vertices in the connected component.

Snuke's profit is ((score)) - ((the sum of costs)). Find the maximum possible profit Snuke can gain.

Constraints

  • 1N3001 \leq N \leq 300
  • 1M3001 \leq M \leq 300
  • 1Ai1061 \leq A_i \leq 10^6
  • 106Bi106-10^6 \leq B_i \leq 10^6
  • 1Ui,ViN1 \leq U_i,V_i \leq N
  • The given graph does not contain self loops or multiple edges.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 A2A_2 \cdots ANA_N

B1B_1 B2B_2 \cdots BNB_N

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

Print the maximum possible profit Snuke can gain.

4 4
4 1 2 3
0 2 -3 1
1 2
2 3
3 4
4 2
1

Deleting Vertex 22 costs 11. After that, the graph is separated into two connected components. The score of the component consisting of Vertex 11 is 0=0|0| = 0. The score of the component consisting of Vertices 33 and 44 is (3)+1=2|(-3) + 1| = 2. Therefore, Snuke's profit is 0+21=10 + 2 - 1 = 1. He cannot gain more than 11, so the answer is 11.

10 12
733454 729489 956011 464983 822120 364691 271012 762026 751760 965431
-817837 -880667 -822819 -131079 740891 581865 -191711 -383018 273044 476880
3 1
4 1
6 9
3 8
1 6
10 5
5 6
1 5
4 3
7 1
7 4
10 3
2306209
4 2
1 1 1 1
1 1 -1 -1
1 2
3 4
4

The given graph is not necessarily connected.