import java.util.*;
public class Main {
private static class Pair{
int a, b;
public Pair(int a, int b) {
this.a=a;
this.b=b;
}
}
public static void main
(String[] args
) { Scanner scanner
= new Scanner
(System.
in); int n = scanner.nextInt();
int na = scanner.nextInt();
int[] A = new int[na];
int[] times = new int[n];
PriorityQueue<Pair> q = new PriorityQueue<>((a, b)->a.a-b.a);
for(int i=0; i<na; i++) {
A[i] = scanner.nextInt();
q.add(new Pair(A[i], A[i]));
}
for(int i=0; i<n; i++) {
Pair pair=q.poll();
times[i]=pair.a;
q.add(new Pair(pair.a+pair.b, pair.b));
}
q.clear();
int nb = scanner.nextInt();
int[] B = new int[nb];
int max_time=0;
for(int i=0; i<nb; i++) {
B[i] = scanner.nextInt();
q.add(new Pair(B[i], B[i]));
}
for(int i=n-1; i>=0; i--) {
Pair pair=q.poll();
times[i]+=pair.a;
q.add(new Pair(pair.a+pair.b, pair.b));
if(times[i]>max_time)
max_time=times[i];
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoJCglwcml2YXRlIHN0YXRpYyBjbGFzcyBQYWlyewoJCWludCBhLCBiOwoJCQoJCXB1YmxpYyBQYWlyKGludCBhLCBpbnQgYikgewoJCQl0aGlzLmE9YTsKCQkJdGhpcy5iPWI7CgkJfQoJCQoJfQoJCglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgewoJCVNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CgkJaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsKCQlpbnQgbmEgPSBzY2FubmVyLm5leHRJbnQoKTsKCQlpbnRbXSBBID0gbmV3IGludFtuYV07CgkJaW50W10gdGltZXMgPSBuZXcgaW50W25dOwoJCVByaW9yaXR5UXVldWU8UGFpcj4gcSA9IG5ldyBQcmlvcml0eVF1ZXVlPD4oKGEsIGIpLT5hLmEtYi5hKTsKCQlmb3IoaW50IGk9MDsgaT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