You Belong With Me


Submit solution

Points: 1
Time limit: 3.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python

This problem was inspired by a debate between Parsa and Caterpillow. Don't worry, though, Caterpillow doesn't know about it.

Caterpillow, for the record, you have officially lost our debate. Checkmate.

Now, onto the actual problem.

Ada wants to choose a group of people for an important project. Every person has an intelligence value, and Ada wants the total intelligence of the chosen group to be as large as possible.

There is just one problem: people have very strong opinions about cities.

Each person belongs to exactly one of these two groups:

  • people who believe Sydney > Melbourne;
  • people who believe Melbourne > Sydney.

Any two people from the same group can work together peacefully. However, people from different groups may or may not be able to work together. You are given the cross-group pairs that can work together.

Ada wants to choose some people such that every two chosen people can work together. Find the maximum possible total intelligence of such a group.

Input

The first line contains three integers n, m, and k (1 \leq n, m \leq 400, 0 \leq k \leq n\times m):

  • n is the number of people who believe Sydney > Melbourne;
  • m is the number of people who believe Melbourne > Sydney;
  • k is the number of known compatible pairs between the two groups.

The next k lines each contain two integers a and b, meaning person a from the Sydney > Melbourne group can work together with person b from the Melbourne > Sydney group.

Then, the next line contains n integers S_1, \dots, S_n (1 \leq S_i \leq 10^9). The i-th integer is the intelligence value of person i from the Sydney > Melbourne group.

Finally, the next line contains m integers M_1, \dots, M_m (1 \leq M_i \leq 10^9). The j-th integer is the intelligence value of person j from the Melbourne > Sydney group.

Output

Output one integer: the maximum total intelligence of a group Ada can choose such that every two chosen people can work together.

Sample Input 1

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

Sample Output 1

8

Sample Input 2

2 3 1
1 1
1 1
5 5 5

Sample Output 2

15

Explanation

In the second sample, the best choice is to select only the three people from the Melbourne > Sydney group. Their total intelligence is 5 + 5 + 5 = 15.


Comments


  • 0
    LUGubrious  commented on May 23, 2026, 7:00 a.m.

    light work no reaction