#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x,"r",stdin)
#define WF(x) freopen(x,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
const LL MOD = 1e9+7;
const int SIZE = 1e6+9;
const LL INF = 1LL<<60;
const double eps = 1e-4;
const double PI=3.1415926535897932;

typedef long long ll;

struct Edge {
    int to;
    ll cap, flow;
    Edge(int tar, ll capacity): to(tar), cap(capacity), flow(0) {}
    ll amt() { return cap-flow; }
};
struct PushRelabel {
    int N;
    vector<Edge> E;
    vector<vector<int> >G;
    vector<int> height, cnt;
    vector<bool> inq;
    vector<ll> excess;
    queue<int> q;
    
    PushRelabel(int _N): N(_N), height(_N), cnt(2*_N), inq(_N), excess(_N), G(_N) {}
    
    void AddEdge(int a, int b, ll c) {  //directed edge a -> b with capacity c
        if (a == b) return;
        G[a].push_back(E.size());
        E.push_back({b, c});
        G[b].push_back(E.size());
        E.push_back({a, 0});
    }
    
    void Enqueue(int x) {
        if (inq[x] || excess[x] == 0) return;
        inq[x] = 1;
        q.push(x);
    }
    
    void Push(int x, int ed) {
        int it = E[ed].to;
        if (height[x] <= height[it]) return;
        ll flow = min(excess[x], E[ed].amt());
        if (flow == 0) return;
        E[ed].flow += flow;
        E[ed^1].flow -= flow;
        excess[x] -= flow;
        excess[it] += flow;
        Enqueue(it);
    }
    
    void Relabel(int x) {
        if (excess[x] == 0) return;
        --cnt[height[x]];
        height[x] = 2*N;
        for (auto it:G[x]) {
            if (E[it].amt() == 0) continue;
            height[x] = min(height[x], height[E[it].to]+1);
        }
        ++cnt[height[x]];
        Enqueue(x);
    }
    
    void GapHeuristic(int g) {
        for (int i = 0; i < N; ++i) {
            if (height[i] >= g) {
                --cnt[height[i]];
                height[i] = max(height[i], N+1);
                ++cnt[height[i]];
                Enqueue(i);
            }
        }
    }
    
    void Discharge(int x) {
        for (auto it:G[x]) {
            Push(x, it);
            if (excess[x] == 0) return;
        }
        if (cnt[height[x]] == 1) GapHeuristic(height[x]);
        else Relabel(x);
    }
    
    ll MaxFlow(int S, int T) {
        height[S] = N;
        cnt[0] = N-1, cnt[N] = 1;
        inq[S] = inq[T] = 1;
        
        for (auto it:G[S]) {
            excess[S] += E[it].cap;
            Push(S, it);
        }
        
        while (!q.empty()) {
            int x = q.front();
            q.pop(), inq[x] = 0;
            Discharge(x);
        }
        
        return excess[T];
    }
    
    vector<int> MinCut(int S, int T) {
        MaxFlow(S, T);
        int split = N;
        vector<int> ans;
        for (int i = 1; i <= N; ++i) {
            if (cnt[i] == 0) {
                split = i;
                break;
            }
        }
        for (int i = 0; i < N; ++i) {
            if (height[i] > split) ans.push_back(i);
        }
        return ans;
    }
};

int a[3009],b[3009];
int l[109];

int main(){
    DRI(n);
    REP(i,n)RI(a[i]);
    LL ans = 0;
    PushRelabel pr = PushRelabel(n+2);
    REP(i,n){
        RI(b[i]);
        if(b[i]>0){
            ans+=b[i];
            pr.AddEdge(n,i,b[i]);
        }
        else{
            pr.AddEdge(i,n+1,-b[i]);
        }
    }
    MS1(l);
    REP(i,n){
        REPP(j,1,a[i]+1){
            if(a[i]%j||l[j]==-1)continue;
            pr.AddEdge(i,l[j],1LL<<50);
        }
        l[a[i]]=i;
    }
    printf("%lld",ans - pr.MaxFlow(n,n+1));
}
