import bisect def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]); idx +=1 A = int(input[idx]); idx +=1 B = list(map(int, input[idx:idx+A])) idx +=A C = int(input[idx]); idx +=1 D = list(map(int, input[idx:idx+C])) idx +=C # Sort A's cards in ascending order for bisect B_sorted = sorted(B) # Generate C's card sequence: sorted, and repeated as per N and C's count D_sorted = sorted(D) c = len(D_sorted) # Create the list of C's cards to be used in each match base = N // c remainder = N % c c_sequence = [] for i in range(c): count = base if i < remainder: count +=1 c_sequence.extend([D_sorted[i]] * count) # Sort the sequence to have all smallest C's cards first c_sequence.sort() wins = 0 # For each card in C's sequence, find the smallest A's card that can beat it for d in c_sequence: # Find the first index in B_sorted where B > d pos = bisect.bisect_right(B_sorted, d) if pos < len(B_sorted): wins +=1 # To avoid reusing the same card, but since cards can be reused, no need to remove # However, in this problem, cards can be reused infinitely, so no need to track usage print(wins) if __name__ == "__main__": main()