Business, business
Difficulty: Hard

Making business and earning money, this is a fashion which will never get old. You, a business man , want to increase 
your fortune of making good business. There are two companies in the town. Each of these companies has a set of services and they are willing to sell these services to you. You know how much you will earn acquiring each of services. But your sharpen business mind figured out something. There are some services from one company you can not buy together with some services from the other company because people will just use one of them. 


**Input specification**

The first line contains N (1<=N<=200) - the number of services one company is selling, M (1<=M<=200) - the number of 
services the other company is selling and K (1<=K<=5000). Next follow two lines. The first one contains N integers Ai, 
(0<=Ai<=100) A1, A2, ... AN-1, AN, it describes the incomes will generate if the i- th service from one company is 
acquired. The second one contains M integers Bi (0<=Bi<=100), B1, B2, ... BM-1, BM, it describes the incomes will 
generate if the i- th service is adquired from the other company. Finally follow K distinct lines in the form 'X Y' 
(1<=X<=N) and (1<=Y<=M), it is not convenient to buy service X from one company and service Y from the other company 
due to the previous reasons.  

**Output specification**

Output just one line, the maximum profit of the business.


**Sample input**
<br>1 1 1
<br>10
<br>15
<br>1 1

**Sample output**
<br>15

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;

import javax.naming.spi.DirectoryManager;
import javax.swing.Box.Filler;



public class Main {	
	static int MAXA = 100000;
	static int MAXV = 500;
	static int oo = (int) 1e9;
	
	static int A, V, source, dest;
	
	static int cap[], flow[], ady[], next[], last[];
	static int now[], level[], Q[];
	
	static void add(int u, int v, int c){
		cap[A] = c;flow[A] = 0;ady[A] = v;next[A] = last[u];last[u] = A++;
		cap[A] = 0;flow[A] = 0;ady[A] = u;next[A] = last[v];last[v] = A++;
	}
	
	static void init() {
		cap = new int[MAXA];
		flow = new int[MAXA];
		ady = new int[MAXA];
		next = new int[MAXA];
		last = new int[MAXV];
		now = new int[MAXV];
		level = new int[MAXV];
		Q = new int[MAXV];
	}
	
	static boolean BFS() {
		int beg, end, u, v;
		for(int i = 1;i<=V;i++)
			level[i] = -1;
		level[source] = beg = end = 0;
		Q[end++] = source;
		
		while(beg < end && level[dest] == -1) {
			u = Q[beg++];
			for(int i = last[u];i!=-1;i=next[i]) {
				v = ady[i];
				if(level[v] == -1 && flow[i] < cap[i]) {
					level[v] = level[u] + 1;
					Q[end++] = v;
				}
			}
		}		
		
		return level[dest] != -1;
	}
	
	static int DFS(int u, int aum) {
		int v;
		if(u == dest) return aum;
		for(int i = now[u];i!=-1;now[u]=i=next[i]) {
			v = ady[i];
			if(level[v] > level[u] && flow[i] < cap[i]){
				int res = DFS(v, Math.min(aum, cap[i]-flow[i]));
				if(res>0) {
					flow[i]+=res;
					flow[i^1]-=res;
					return res;
				}
			}
		}
		return 0;
	}
	
	static int Dinic() {
		int ff = 0, res;
		while(BFS()){
			for(int i = 0;i<=V;i++) 
				now[i] = last[i];
			while((res = DFS(source, oo))>0)
				ff+=res;
		}
		return ff;
	}
	
	
	
	public static void main(String[] args) throws IOException {
		int N, M, K, x, y;
		
		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(sr);
		
		String cad[] = br.readLine().split(" ");
		N = Integer.parseInt(cad[0]);
		M = Integer.parseInt(cad[1]);
		K = Integer.parseInt(cad[2]);
		
		int total = 0;
		
		source = 0;
		dest = N + M + 1;
		V = dest;
		A = 0;
		init();
		
		for(int i = 0;i<=V;i++)
			last[i] = -1;		
		
		cad = br.readLine().split(" ");
		for (int i = 0; i < N; i++) {
			x = Integer.parseInt(cad[i]);
			add(source, i+1, x);
			total += x;
		}
				
		cad = br.readLine().split(" ");
		for (int i = 0; i < M; i++) {
			x = Integer.parseInt(cad[i]);
			add(N + i + 1, dest, x);
			total += x;
		}
		
		while(K-->0) {
			cad = br.readLine().split(" ");
			x = Integer.parseInt(cad[0]);
			y = Integer.parseInt(cad[1]);
			add(x, N + y, oo);
		}		
		
		System.out.println(total - Dinic());		
	}
}