#include<iostream>
#include<vector>
using namespace std;
long long n,m,s,x,b[20005];
long long solve(long long dep,vector<long long> v)
{
if(dep>m){
long long tot=0;
for(auto x:v) tot+=x;
return tot;
}
if(!v.size()) return 0;
vector<long long> p,q;
for(auto x:v){
if(x%b[dep]) p.push_back(x);
else q.push_back(x);
}
if(dep%2) return min(solve(dep+1,p),solve(dep+1,q));
else return max(solve(dep+1,p),solve(dep+1,q));
}
int main(){
cin>>n>>m;
vector<long long> V;
if(m>100) {cout<<"0\n"; return 0;}
for(long long i=1;i<=n;i++) {cin>>x; v.push_back(x); s+=x;}
for(long long i=1;i<=m;i++) cin>>b[i];
cout<<solve(1,V)<<"\n";
return 0;
}