Type: Default 1000ms 256MiB

F - Sum of Abs

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Score : 900 points

Problem Statement

Given is a simple undirected graph with N vertices and M edges. Its vertices are numbered 1,2,,N and its edges are numbered 1,2,,M. On Vertex i (1iN) two integers Ai and Bi are written. Edge i (1iM) connects Vertices Ui and Vi.

Snuke picks zero or more vertices and delete them. Deleting Vertex i costs Ai. 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 Bi 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

  • 1N300
  • 1M300
  • 1Ai106
  • 106Bi106
  • 1Ui,ViN
  • 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:

N M
A1 A2  AN
B1 B2  BN
U1 V1
U2 V2

UM VM

Output

Print the maximum possible profit Snuke can gain.


Sample Input 1

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

Sample Output 1

1

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


Sample Input 2

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

Sample Output 2

2306209

Sample Input 3

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

Sample Output 3

4

The given graph is not necessarily connected.

排位赛1

Not Attended
Status
Done
Rule
IOI
Problem
8
Start at
2023-7-11 8:00
End at
2023-7-11 12:00
Duration
4 hour(s)
Host
Partic.
21