#include<bits/stdc++.h> 
using namespace std;
namespace Sakurajima_Mai{
	//#pragma GCC optimize(2)
    //#pragma G++ optimize(2)
	#define ms(a) memset(a,0,sizeof(a))
	#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
	#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
	#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
	#define ce(i,r) i==r?'\n':' '
	#define pb push_back 
	#define all(x) x.begin(),x.end()
	#define gmn(a,b) a=min(a,b)
	#define gmx(a,b) a=max(a,b)
	#define fi first
	#define se second   					  
	typedef long long ll;
	typedef unsigned long long ull;
	typedef double db;
	const int infi=2e9;//infi½Ï´ó£¬×¢ÒâÉæ¼°infÏà¼ÓÊ±±¬int
	const ll infl=4e18;
	inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
	//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
	inline int qi(){
	    int f=0,fu=1; char c=getchar();
	    while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
	    while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
	    return f*fu;
	}
	inline ll ql(){
		ll f=0;int fu=1; char c=getchar();
	    while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
	    while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
	    return f*fu;
	}
	inline db qd(){
	    char c=getchar();int flag=1;double ans=0;
	    while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
	    if(c=='-') flag=-1,c=getchar();
	    while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
	    if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
	    return ans*flag;
	}
}
namespace Read{
	#define si(a) scanf("%d",&a)
	#define sl(a) scanf("%lld",&a)
	#define sd(a) scanf("%lf",&a)
	#define ss(a) scanf("%s",a)
	#define rai(x,a,b) repi(i,a,b) x[i]=qi()
	#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
	#define pi(x) printf("%d",x)
	#define pl(x) printf("%lld",x)
	#define ps(x) printf("%s",x)
	#define pc(x) printf("%c",x)
	#define pe() puts("")
}
namespace DeBug{
	#define MARK false
	#define DB if(MARK) 
	#define pr(x) cout<<#x<<": "<<x<<endl
	#define pra(x,a,b) cout<<#x<<": "<<endl; \
			repi(i,a,b) cout<<x[i]<<" ";	 \
			puts("");   
	#define FR(a) freopen(a,"r",stdin)
	#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
const int MAX_V=3e3+10;
const int MAX_E=MAX_V+3e5;//½¨ÁË·´±ß ´óÐ¡<<1
struct Edge{int from,to,nxt;ll cap;}e[MAX_E];
int head[MAX_V],tote;
int s,t,V;
int dep[MAX_V],gap[MAX_V];
void add_edge(int from,int to,ll cap)
{
	e[++tote].from=from,e[tote].to=to,e[tote].cap=cap;
	e[tote].nxt=head[from],head[from]=tote;
	e[++tote].from=to,e[tote].to=from,e[tote].cap=0;//??? e[tote].cap=cap
	e[tote].nxt=head[to],head[to]=tote;
}
int que[MAX_V],front=0,rear=0;
void BFS(int start,int end)
{
	repi(i,0,V) dep[i]=-1,gap[i]=0; gap[0]=1;
    front=0,rear=0;
    dep[end]=0,que[rear++]=end;
    while(front!=rear){
        int u=que[front++];
        if(front==MAX_V)front=0;
        reps(u)if(dep[v]==-1){
            que[rear++]=v;
            if(rear==MAX_V)rear=0;
            dep[v]=dep[u]+1,++gap[dep[v]];
        }
    }
}
int cur[MAX_V],S[MAX_V],top=0;
ll SAP(int start,int end)
{
    ll res=0;
    BFS(start,end);
    top=0;
    repi(i,0,V) cur[i]=head[i];
    int u=start,i;
    while(dep[start]<V){
        if(u==end){
            ll temp=infl;
            int inser;
            repi(j,0,top-1)if(temp>e[S[j]].cap) temp=e[S[j]].cap,inser=j;
            repi(j,0,top-1) e[S[j]].cap-=temp,e[S[j]^1].cap+=temp;
            res+=temp,top=inser,u=e[S[top]].from;
        }
        
        if(u!=end&&gap[dep[u]-1]==0) break;
        for(i=cur[u];i;i=e[i].nxt)if(e[i].cap!=0&&dep[u]==dep[e[i].to]+1) break;
        if(i) cur[u]=S[top++]=i,u=e[i].to;
        else{
            int min=V;
            for(i=head[u];i;i=e[i].nxt)if(e[i].cap&&min>dep[e[i].to])
                min=dep[e[i].to],cur[u]=i;
            --gap[dep[u]],dep[u]=min+1,++gap[dep[u]];
            if(u!=start) u=e[S[--top]].from;
        }
    }
    return res;
}
void init(int n)//n ×ÜµãÊý+5
{
	repi(i,0,n) head[i]=0; tote=1,V=n;
}
int a[MAX_V],b[MAX_V];
bool vis[105];
int main()
{
	int n=qi(); rai(a,1,n); rai(b,1,n);
	init(n+5),s=n+1,t=n+2;
	ll ans=0;
	repi(i,1,n){
		if(b[i]>=0) add_edge(s,i,b[i]),ans+=b[i];
		else add_edge(i,t,-b[i]);
	}
	repd(i,n,1){
		repi(j,1,100) vis[j]=false;
		repi(j,i+1,n)if(a[j]%a[i]==0){
			bool flag=false;
			repi(k,1,100)if(vis[k]&&a[j]%k==0){ flag=true; break; }
			if(flag) continue;
			vis[a[j]]=true,add_edge(j,i,infl);
		}
	}
	ans-=SAP(s,t);
	pl(ans),pe();	
	return 0;
}