#include<bits/stdc++.h>
#define inf (ll)1e9
#define inff 0x3f3f3f3f
#define mem(x) memset(x,0,sizeof x)
#define ll long long
using namespace std;
int num=-1,n,m,s,t,cur[3010],head[3010],deep[3010];
int val[3010],a[3010];
ll ans;
bool mapp[110];
struct Edge{
	int to,dis,next;
}edge[400010];
void add(int f,int t,int dis){
	//cout<<"#"<<f<<" "<<t<<" "<<dis<<endl;
	edge[++num]={t,dis,head[f]};head[f]=num;
	edge[++num]={f,0,head[t]};head[t]=num;
}
queue<int>q;
bool bfs(int s,int t){
	for(int i=s;i<=t;i++) cur[i]=head[i];
	memset(deep,inff,sizeof deep);
	while(!q.empty()) q.pop();
	q.push(s);deep[s]=0;
	while(!q.empty()){
		int nowp=q.front();q.pop();
		for(int i=head[nowp];~i;i=edge[i].next){
			if(deep[edge[i].to]==inff&&edge[i].dis){
				deep[edge[i].to]=deep[nowp]+1;
				q.push(edge[i].to);
			}
		}
	}
	return deep[t]!=inff;
}
int dfs(int nowp,int t,int lim){
	if(nowp==t||lim==0) return lim;
	int f,ret=0;
	for(int i=cur[nowp];~i;i=edge[i].next){
		cur[nowp]=i;
		if(deep[edge[i].to]==deep[nowp]+1&&(f=dfs(edge[i].to,t,min(lim,edge[i].dis)))){
			ret+=f,lim-=f;
			edge[i].dis-=f,edge[i^1].dis+=f;
			if(!lim) break;
		}
	}
	return ret;
}
void dinic(int s,int t){
	while(bfs(s,t)) ans+=dfs(s,t,inf);
}
int get_num(int x,bool fg){//fg==0 set  fg==1  id
	return fg*n+x;
}
int main(){
	ll sum=0;
	memset(head,-1,sizeof head);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}s=0,t=n+1;
	for(int i=1;i<=n;i++){
		scanf("%d",&val[i]);
	}
	for(int i=1;i<=n;i++){
		if(val[i]>=0){
			add(s,i,val[i]);sum+=val[i];
		}
		else add(i,t,-val[i]);
	}
	for(int i=1;i<=n;i++){
		mem(mapp);
		for(int j=i-1;j>=1;j--){
			if(a[i]%a[j]==0&&!mapp[a[j]]) {
				add(i,j,inf) ;
				mapp[a[j]]=1;
			}
		}
	}
	dinic(s,t);
	//cout<<anss<<endl;
	printf("%lld\n",sum-ans);
}//？