#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) ((int)x.size())
#define lowbit(x) x&-x
#define pb push_back
#define ALL(x) (x).begin(),(x).end()
#define UNI(x) sort(ALL(x)),x.resize(unique(ALL(x))-x.begin())
#define GETPOS(c,x) (lower_bound(ALL(c),x)-c.begin())
#define LEN(x) strlen(x)
#define MS0(x) memset((x),0,sizeof((x)))
#define Rint register int
#define ls (u<<1)
#define rs (u<<1|1)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<pii> Vpii;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(ull &x) { scanf("%llu", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const ull &x) { printf("%llu", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.fi);putchar(' '); _W(x.se);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
void WW() {}
template<class T, class... U> void WW(const T &head, const U &... tail) {
    #ifndef ONLINE_JUDGE
    _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...);
    #endif
}
const int MOD=998244353,mod=998244353;
ll qpow(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
const int MAXN=4005+10,MAXM=4e5+10;
const int INF=INT_MAX,SINF=0x3f3f3f3f;
const ll llINF=LLONG_MAX;
const int inv2=(MOD+1)/2;
const int Lim=1<<20;

int a[3005];
ll b[3005],ans;
int n;

struct dinic
{
    struct Edge
    {
        int to,nxt,flow;
    }e[MAXM];
    int head[MAXN],ecnt=-1,dis[MAXN],cur[MAXN];
    void add_edge(int u,int v,int f)
    {
        e[++ecnt]={v,head[u],f};head[u]=ecnt;
        e[++ecnt]={u,head[v],0};head[v]=ecnt;
    }
    int n,m,s,t;
    bool bfs()
    {
        memset(dis,0,sizeof(dis));
        queue<int> q;
        dis[s]=1;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();q.pop();
            for(int i=head[u];~i;i=e[i].nxt)
            {
                int v=e[i].to;
                if(!dis[v]&&e[i].flow>0)
                {
                    dis[v]=dis[u]+1;
                    q.push(v);
                }
            }
        }
        return dis[t];
    }
    int dfs(int u,int flow)
    {
        if(u==t)return flow;
        int nowflow=0;
        for(int &i=cur[u];~i;i=e[i].nxt)
        {
            int v=e[i].to;
            if(dis[v]==dis[u]+1&&e[i].flow>0)
            {
                int tmp=dfs(v,min(flow,e[i].flow));
                e[i].flow-=tmp;
                e[i^1].flow+=tmp;
                flow-=tmp;
                nowflow+=tmp;
                if(!flow)break;
            }
        }
        return nowflow;
    }
    ll solve()
    {
        ll ans=0;
        while(bfs())
        {
            memcpy(cur,head,(n+5)*sizeof(int));
            ans+=dfs(s,INF);
        }
        return ans;
    }
    void init(int S,int T,int N)
    {
        ecnt=-1;
        s=S;t=T;n=N;
        memset(head,-1,sizeof(head));
    }
}G;

int lst[105];

void solve()
{
    R(n);G.init(n+1,n+2,n+2);
    for(int i=1;i<=n;i++)R(a[i]);
    for(int i=1;i<=n;i++)R(b[i]);
    for(int i=1;i<=n;i++)
    {
        if(b[i]>0)G.add_edge(G.s,i,b[i]),ans+=b[i];
        else G.add_edge(i,G.t,-b[i]);
        for(int j=1;j<=a[i];j++)
            if(a[i]%j==0)G.add_edge(i,lst[j],INF);
        lst[a[i]]=i;
    }
    W(ans-G.solve());
}

int main()
{
    //freopen("input.txt","w",stdout);
    //freopen("test_output.txt","w",stdout);
    int T=1;
    //R(T);
    for(int kase=1;kase<=T;kase++)
    {
        //printf("Case #%d: ",kase);
        solve();
    }
    return 0;
}
