#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<climits>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
    int f=1,ans=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return f*ans;
}
const int MAXN=3e3+11;
int head[MAXN],tot,cnt; struct Edge{int u,v,w,nex;}E[MAXN*101*2];
void add(int u,int v,int w){
    cerr<<"u:"<<u<<" v:"<<v<<" w:"<<w<<endl;
    E[cnt].u=u,E[cnt].v=v,E[cnt].w=w,E[cnt].nex=head[u],head[u]=cnt++; swap(u,v),w=0;
    E[cnt].u=u,E[cnt].v=v,E[cnt].w=w,E[cnt].nex=head[u],head[u]=cnt++;return;
}
int dis[MAXN],S,T; queue<int> que;
bool bfs(){
    memset(dis,127/3,sizeof(dis)); int inf=dis[0]; dis[S]=0; que.push(S);
    while(!que.empty()){
        int xx=que.front(); que.pop();
        for(int i=head[xx];i!=-1;i=E[i].nex){
            int v=E[i].v,w=E[i].w; if(!w) continue;
            if(dis[v]>dis[xx]+1){dis[v]=dis[xx]+1,que.push(v);}
        }
    }return dis[T]!=inf;
}
int dfs(int u,int flow){
    if(u==T) return flow;
    int used=0; for(int i=head[u];i!=-1;i=E[i].nex){
        int v=E[i].v,w=E[i].w; if(dis[v]==dis[u]+1&&w){
            int slow=dfs(v,min(flow-used,w)); E[i].w-=slow,E[i^1].w+=slow;
            used+=slow; if(used==flow) break;
        }
    }if(!used) dis[u]=-1; return used;
}
int dinic(){int Ans=0; while(bfs()) Ans+=dfs(S,INT_MAX); return Ans;}
vector<int> vec[111]; int N,A[MAXN],W[MAXN];
int main(){
    //freopen("A.in","r",stdin);
    memset(head,-1,sizeof(head));
    N=read(); for(int i=1;i<=N;i++) A[i]=read(); for(int i=1;i<=N;i++) W[i]=read();
    for(int i=1;i<=N;i++) vec[A[i]].pb(i);
    for(int i=1;i<=N;i++){
        int x=A[i];
        for(int j=1;j<=100;j++) if(!(x%j)){
            int l=0,r=vec[j].size()-1,res=-1;
            while(l<=r){
                int mid=(l+r)>>1;
                if(vec[j][mid]<i) l=mid+1,res=mid;
                else r=mid-1;
            }
            if(res!=-1) add(i,vec[j][res],INT_MAX);
        }
    }
    S=0,T=N+1; int res=0;
    for(int i=1;i<=N;i++){
        if(W[i]>=0) res+=W[i],add(S,i,W[i]);
        else add(i,T,-W[i]);
    }
    printf("%d\n",res-dinic());
}

