import java.io.*;
import java.util.*;
class ANARC05B
{
public static void main
(String args
[]) {
try{
while(!(str1=br.readLine()).equals("0"))
{
int arr1
[]=new int[Integer.
parseInt(st1.
nextToken())]; int arr2
[]=new int[Integer.
parseInt(st2.
nextToken())]; int pos=0;
ArrayList<Integer> al=new ArrayList<Integer>();
ArrayList<Integer> list=new ArrayList<Integer>();
while(st1.hasMoreTokens()){
arr1
[pos
++]=Integer.
parseInt(st1.
nextToken());al.
add(arr1
[pos
-1]);} pos=0;
while(st2.hasMoreTokens()){
arr2
[pos
++]=Integer.
parseInt(st2.
nextToken()); if(al.contains(arr2[pos-1]))
list.add(arr2[pos-1]);
}
int sum1=0,sum2=0,beg=0, main_sum=0,p1=0,p2=0;
//if there are no intersections at all
if(list.isEmpty())
{
if(arr1.length<arr2.length)
{
for(int i=0;i<arr1.length;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
for(int i=arr1.length;i<arr2.length;i++)
sum2+=arr2[i];
main_sum
+=Math.
max(sum1, sum2
); continue;
}
else if(arr2.length<arr1.length)
{
for(int i=0;i<arr2.length;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
for(int i=arr2.length;i<arr1.length;i++)
sum1+=arr1[i];
main_sum
+=Math.
max(sum1, sum2
); continue;
}
else//same length of both arrays
{
for(int i=0;i<arr2.length;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
main_sum
+=Math.
max(sum1, sum2
); continue;
}
}
Iterator<Integer> iter=list.iterator();
while(iter.hasNext())
{
int intersect=iter.next();
p1
=Arrays.
binarySearch(arr1, intersect
); p2
=Arrays.
binarySearch(arr2, intersect
); if(p1==p2)
{
for(int i=beg;i<=p1;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
main_sum
+=Math.
max(sum1, sum2
); beg=p1+1;
}
else if(p1>p2)
{
for(int i=beg;i<=p2;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
for(int i=p2+1;i<=p1;i++)
sum1+=arr1[i];
main_sum
+=Math.
max(sum1, sum2
); beg=p1+1;
}
else if(p2>p1)
{
for(int i=beg;i<=p1;i++)
{
sum1+=arr1[i];
sum2+=arr2[i];
}
for(int i=p1+1;i<=p2;i++)
sum2+=arr2[i];
main_sum
+=Math.
max(sum1, sum2
); beg=p2+1;
}
sum1=sum2=0;
if(beg-p1<beg-p2)
{
for(int i=beg-1;i>p2;i--)
sum2+=arr2[i];
}
else if(beg-p1>beg-p2)
{
for(int i=beg-1;i>p1;i--)
sum1+=arr1[i];
}
}
//calculating from last intersection point till the end
sum1=sum2=0;
for(int i=p1+1;i<arr1.length;i++)
sum1+=arr1[i];
for(int i=p2+1;i<arr2.length;i++)
sum2+=arr2[i];
main_sum
+=Math.
max(sum1, sum2
); }
}//try block
{return;}
}
}
CgoJCQlpbXBvcnQgamF2YS5pby4qOwoJCQlpbXBvcnQgamF2YS51dGlsLio7CgkJCWNsYXNzIEFOQVJDMDVCCgkJCXsKCQkJCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZyBhcmdzW10pCgkJCQl0aHJvd3MgSU9FeGNlcHRpb24KCQkJCXsKCQkJCQlCdWZmZXJlZFJlYWRlciBicj1uZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwoJCQkJCVN0cmluZyBzdHIxPSIiOwoJCQkJCXRyeXsKCQkJCQl3aGlsZSghKHN0cjE9YnIucmVhZExpbmUoKSkuZXF1YWxzKCIwIikpCgkJCQkJewoJCQkJCQlTdHJpbmcgc3RyMj1ici5yZWFkTGluZSgpOwoJCQkJCQlTdHJpbmdUb2tlbml6ZXIgc3QxPW5ldyBTdHJpbmdUb2tlbml6ZXIoc3RyMSwiICsiKTsKCQkJCQkJU3RyaW5nVG9rZW5pemVyIHN0Mj1uZXcgU3RyaW5nVG9rZW5pemVyKHN0cjIsIiArIik7CgkJCQkJCWludCBhcnIxW109bmV3IGludFtJbnRlZ2VyLnBhcnNlSW50KHN0MS5u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