#include<bits/stdc++.h>
#define mem(X) memset(X,0,sizeof(X))
#define mes(X,Y) memset(X,Y,sizeof(X))
#define dd(X) if (DBG) cout<<#X<<"="<<X<<' ';
#define de(X) if (DBG) cout<<#X<<"="<<X<<'\n';
#define fi first
#define se second
#define pb push_back
#define sz(X) ( (int)(X.size()) )
#define rep(i,a,b) for(int i=(a);i<=(b);++i)//左闭右闭
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define pii pair<ll,ll>
using namespace std;
typedef long long ll;
const int DBG=true;
const int SI=2e5+100;
const ll mod=1e9+7;
inline ll pow_mod(ll X,ll N,ll mod){ll res=1;while(N>0){if (N&1)res=res*X%mod;X=X*X%mod;N>>=1;}return res;}
struct Network_flow{
#define MAX_V 3005
#define INF 0x3f3f3f3f
	struct edge{
	int to,cap,rev;
	edge(){}
	edge(int to_,int cap_,int rev_):to(to_),cap(cap_),rev(rev_){}
	};
	vector<edge> G[MAX_V];
	int level[MAX_V],iter[MAX_V];	
	void add_edge(int from,int to,int cap)
	{
		G[from].push_back( edge(to,cap,G[to].size() ));
		G[to].push_back( edge( from,0,G[from].size()-1 ) );
	}
	void bfs(int s)
	{
		mes(level,-1);
		level[s]=0;
		queue<int> q;
		q.push(s);
		while( !q.empty() )
		{
			int t=q.front();q.pop();
			for( int i=0;i<G[t].size();i++ ){
				edge e=G[t][i];
				if ( e.cap>0 && level[e.to]<0 )
				{
					level[ e.to ]=level[t]+1;
					q.push(e.to);	
				} 
			}			
		}	
	}
	int dfs(int u,int t,int f)
	{
		if ( u==t )
			return f;
		for( int &i=iter[u];i<G[u].size();i++ )
			{
				edge &e=G[u][i];
				if ( e.cap>0 && level[e.to]>level[u] )
				{
					int d=dfs(e.to,t,min(f,e.cap));
					if ( d>0 ){
						e.cap-=d;
						G[ e.to ][ e.rev ].cap+=d;
						return d;
					}
				}	
			}
		return 0;
	}
	int max_flow(int s,int t)
	{
		int flow=0;
		bfs(s);
		while( level[t]>0 )
		{
			int f;
			mem(iter);
			while( ( f=dfs(s,t,INF) )>0 )
				flow+=f;
			bfs(s);
		}
		return flow;
	}
}FL;
int n;
int a[3005],b[3005];
int main()
{
	cin>>n;
	int S=0,T=n+1;
	for( int i=1;i<=n;i++ )
		cin>>a[i];
	int sum=0;
	for( int i=1;i<=n;i++ )
		{
			cin>>b[i];
			if ( b[i]>=0 )
				{
					FL.add_edge( S,i,b[i] );
					sum+=b[i];
				}
			else
				FL.add_edge( i,T,-b[i] );
		}
	for( int i=1;i<=n;i++ )
		{
			vector<int> temp;
			for( int j=i-1;j>=1;j-- )
				if ( a[i]%a[j]==0 )
					{
						bool gd=true;
						for( auto it:temp )
							if ( it%a[j]==0 )
								{
									gd=false;
									break; 
								} 
						if ( gd )
							{
								FL.add_edge( i,j,mod );
								temp.pb( a[j] );
							}
					}
		}
	int fl=FL.max_flow(S,T);
	cout<<max(0,sum-fl);
	return 0;
}