#ifndef _MY
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
#endif
#include "bits/stdc++.h"
#define int ll
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
const int64_t INF = (int64_t)(2e18);
const int inf = (int)(1e9 + 7);
const int maxn = 500 * 1000 + 100;
chrono::time_point<chrono::steady_clock> cl;
double current_time() { return (double)(chrono::steady_clock::now() - cl).count() / 1e9; }
//------------------------------------------//

struct Dinic{
    int s, t;
    int graphsize;
    struct edge { 
        int to, cap, f = 0; 
        edge() {}
        edge(int to, int cap) :to(to), cap(cap) {}
    };
    vector<vector<int>> g;
    vector<int> d, cnt;
    vector<edge> E;
    bool bfs() {
        queue<int> q;
        q.push(s);
        cnt.assign(graphsize, 0);
        d.assign(graphsize, inf);
        d[s] = 0;
        while (q.size()) {
            int v = q.front();
            if (v == t) return true;
            q.pop();
            for (auto e : g[v]) {
                if (d[E[e].to] == inf && E[e].f < E[e].cap) {
                    q.push(E[e].to);
                    d[E[e].to] = d[v] + 1;
                }
            }
        }
        return false;
    }
    int dfs(int v, int f) {
        if (v == t) return f;
        for (; cnt[v] < (int)g[v].size(); ++cnt[v]) {
            int e = g[v][cnt[v]];
            int to = E[e].to;
            if (d[to] != d[v] + 1 || E[e].f == E[e].cap) continue;
            int flow = dfs(to, min(f, E[e].cap - E[e].f));
            if (flow) {
                E[e].f += flow;
                E[e ^ 1].f -= flow;
                return flow;
            }
        }
        return 0;
    }
    
    int maxflow() {
        int f = 0;
        int ans = 0;
        while (bfs())
            while ((f = dfs(s, inf)))
                ans += f;
        return ans;
    }
    
    void add_edge(int f, int to, int cap) {
        g[f].push_back(int(E.size()));
        E.push_back(edge(to, cap));
        g[to].push_back(int(E.size()));
        E.push_back(edge(f, 0));
    }
    Dinic(){}
    Dinic(int s, int t, int graphsize):s(s), t(t), graphsize(graphsize){
        g.resize(graphsize);
    }
};


int32_t main(){
    cl = chrono::steady_clock::now();
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cin.tie(nullptr);
#ifdef _MY
    freopen("VimProject/input.txt", "r", stdin);
    freopen("VimProject/output.txt", "w", stdout);
#endif

    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for (auto& it : a) cin >> it;
    for (auto& it : b) cin >> it;

    int res = 0;
    for (auto& it : b) res += max(it, 0ll);

    vector<int> lastpos(101, -1);

    Dinic flows(n, n+1, n+2);
    for (int i = 0; i < n; ++i){
        if (b[i] >= 0) flows.add_edge(flows.s, i, b[i]);
        else flows.add_edge(i, flows.t, abs(b[i]));
    }

    for (int i = 0; i < n; ++i){
        for (int k = 1; k <= 100; ++k){
            if (lastpos[k] == -1) continue;
            if (a[i] % k != 0) continue;
            flows.add_edge(i, lastpos[k], ll(1e11));
        }
        lastpos[a[i]] = i;
    }

    cout << res - flows.maxflow();

    return 0;
}
