#include <bits/stdc++.h>
//#include <immintrin.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
using namespace std;

#define PI 3.141592653589
#define endl "\n"
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define fr(i,a,b) for(int i = (a); i < (b); ++i)

#define vi vector<int>
#define vpi vector<pair<int, int>>

struct edge{
    int a, b, cap, flow;
};

const int N = 4000, INF = 1e9;
int q[N], ptr[N], d[N], s, t, am;
int a[N], b[N];
vector<edge> e;
vector<int> gr[N];

void add_edge(int a, int b, int cap){
    edge e1 = {a, b, cap, 0};
    edge e2 = {b, a, 0, 0};
    gr[a].push_back(sz(e));
    e.push_back(e1);
    gr[b].push_back(sz(e));
    e.push_back(e2);
}

bool bfs(){
    int qh = 0, qt = 0;
    memset(d, -1, am * sizeof d[0]);
    q[qt++] = s;
    d[s] = 0;
    while(qh < qt && d[t] == -1){
        int v = q[qh++];
        for(int id : gr[v]){
            int u = e[id].b;
            if(d[u] == -1 && e[id].flow < e[id].cap){
                d[u] = d[v] + 1;
                q[qt++] = u;
            }
        }
    }
    return d[t] != -1;
}

int dfs(int v, int flow){
    if(!flow) return 0;
    if(v == t) return flow;
    for(;ptr[v] < sz(gr[v]); ptr[v]++){
        int id = gr[v][ptr[v]], 
            u = e[id].b;
        if(d[u] != d[v] + 1) continue;
        int pushed = dfs(u, min(flow, e[id].cap - e[id].flow));
        if(pushed){
            e[id].flow += pushed;
            e[id^1].flow -= pushed;
            return pushed;
        }
    }
    return 0;
}

int dinic(){
    int flow = 0;
    while(true){
        if(!bfs()) break;
        memset(ptr, 0, am * sizeof ptr[0]);
        while(int pushed = dfs(s, INF))
            flow += pushed;
    }
    return flow;
}

const int cs = 101;
int lst[cs];
vector<int> dd[cs];


void solve(){
    fr(i, 1, 101){
        fr(j, 1, 101){
            if(i % j == 0) dd[i].push_back(j);
        }
    }
    memset(lst, -1, 101 * sizeof lst[0]);
    int n;
    cin >> n;
    fr(i, 0, n) cin >> a[i];
    fr(i, 0, n) cin >> b[i];
    s = 0, t = n + 1, am = n + 2;
    int ans = 0;
    fr(i, 0, n){
        if(b[i] < 0) add_edge(i + 1, t, -b[i]);
        else         add_edge(s, i + 1, b[i]);
        for(int j : dd[a[i]]){
            if(lst[j] != -1) add_edge(i + 1, lst[j] + 1, INF);
        }
        lst[a[i]] = i;
        ans += max(b[i], 0);
    }
    cout << ans - dinic();


}

signed main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0); cin.tie(0);
    int t;
    t = 1;
    //cin >> t;
    while(t--) {solve();}
    return 0;
}
