# マッチング、最大流でできるか、間に合うか # Start, M人M頂点、N商品N頂点、Goal N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) TC = [] for i in range(M): t, c = map(int, input().split()) TC.append((t, c)) Start = 0 Goal = M+N+1 from collections import defaultdict, deque INF = float('inf') edges = defaultdict(set) cost = [[0]*(M+N+2) for i in range(M+N+2)] for i in range(M): t, c = TC[i] for j in range(N): a = A[j] b = B[j] if (t == 0 and a <= c) or (t == 1 and b <= c): edges[i+1].add(j+M+1) cost[i+1][j+M+1] = 1 # from Start to M nodes for i in range(M): edges[Start].add(i+1) cost[Start][i+1] = 1 # from N nodes to Goal for i in range(N): edges[i+M+1].add(Goal) cost[i+M+1][Goal] = 1 ans = 0 def Ford_Fulkerson(s): #sからFord-Fulkerson global edges global cost global ans queue = deque() #BFS用のdeque queue.append([s,INF]) ed = [True]*(M+N+2+1) #到達済み ed[s] = False route = [0 for i in range(M+N+2+1)] #ルート route[s] = -1 #BFS while queue: s,flow = queue.pop() for t in edges[s]: #s->t if ed[t]: flow = min(cost[s][t],flow) #flow = min(直前のflow,line容量) route[t] = s queue.append([t,flow]) ed[t] = False if t == Goal: #ゴール到達 ans += flow break else: continue break else: return False #ラインの更新 t = Goal s = route[t] while s != -1: #s->tのコスト減少,ゼロになるなら辺を削除 cost[s][t] -= flow if cost[s][t] == 0: edges[s].remove(t) #t->s(逆順)のコスト増加,元がゼロなら辺を作成 if cost[t][s] == 0: edges[t].add(s) cost[t][s] += flow t = s s = route[t] return True while True: if Ford_Fulkerson(Start): continue else: break #print(ans) remaining = N-ans print(remaining)