#include "bits/stdc++.h"
using namespace std;

#define sync ios_base::sync_with_stdio(0);cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define mem(a , x) memset(a, x, sizeof a)

const ld pi = atan(1) * 4.0; 


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct FlowEdge {
    int v, u;
    long long cap, flow = 0;
    FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};

struct Dinic {
    const long long flow_inf = 1e18;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;

    Dinic(int n, int s, int t) : n(n), s(s), t(t) {
        adj.resize(n);
        level.resize(n);
        ptr.resize(n);
    }

    void add_edge(int v, int u, long long cap) {
        edges.emplace_back(v, u, cap);
        edges.emplace_back(u, v, 0);
        adj[v].push_back(m);
        adj[u].push_back(m + 1);
        m += 2;
    }

    bool bfs() {
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                if (edges[id].cap - edges[id].flow < 1)
                    continue;
                if (level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }

    long long dfs(int v, long long pushed) {
        if (pushed == 0)
            return 0;
        if (v == t)
            return pushed;
        for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
            int id = adj[v][cid];
            int u = edges[id].u;
            if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }

    long long flow() {
        long long f = 0;
        while (true) {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if (!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while (long long pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        return f;
    }
};

int main(){

    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    sync

    int t = 1;
    //cin >> t;
    while(t--){
        int n; cin >> n;
        vector<int> a(n);
        for (int& x : a) cin >> x;
        vector<int> b(n);
        for (int& x : b) cin >> x;

        Dinic G(n + 3, n + 1, n + 2);

        int idx[111]; mem(idx, -1);
        const ll inf = std::numeric_limits<ll>::max();
        int Src = n + 1, Snk = n + 2;
        ll cnt = 0;
        for (int i = 0;i < n;i++){
            for (int j = a[i]; j >= 1;j--){
                if (~idx[j] && a[i] % j == 0){
                    G.add_edge(i + 1, idx[j] + 1 , inf);
                }
            }
            idx[a[i]] = i;
            if (b[i] >= 0){
                G.add_edge(Src, i + 1 , b[i]);
            }
            else{
                G.add_edge(i + 1, Snk , -b[i]);
            }
            cnt += (b[i] > 0 ? b[i] : 0);
        }

        ll flw = G.flow(); 
        cnt -= flw;
        cout << cnt << "\n";
    }

    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";
    
    return 0;
}