#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

#define ll long long
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define vvpll vector<vector<pair<ll, ll>>>

// moostly copied from https://codeforces.com/contest/1473/submission/104669596
template<typename T>
struct FlowEdge {
    int src, dest;
    T cap, flow;
};
template<typename T>
struct max_flow {
    vector<FlowEdge<T>> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;
    T scaling_lim = (1 << 30);
    T maxT = numeric_limits<T>::max(); // change this

    max_flow(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 src, int dest, T cap) {
        edges.push_back({ src, dest, cap });
        edges.push_back({ dest, src, 0 });
        adj[src].push_back(m);
        adj[dest].push_back(m + 1);
        m += 2;
    }
    bool bfs() {
        fill(level.begin(), level.end(), -1);
        fill(ptr.begin(), ptr.end(), 0);
        level[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                FlowEdge<T> e = edges[id];
                if (e.cap - e.flow < scaling_lim || level[e.dest] != -1) continue;
                level[e.dest] = level[v] + 1;
                q.push(e.dest);
            }
        }
        return level[t] != -1;
    }
    T dfs(int v, T pushed) {
        if (pushed == 0) return 0;
        if (v == t) return pushed;
        for (int& pp = ptr[v]; pp < adj[v].size(); pp++) {
            int id = adj[v][pp];
            FlowEdge<T>& e = edges[id]; FlowEdge<T>& e2 = edges[id ^ 1];
            if (level[e.dest] != level[v] + 1 || e.cap - e.flow < 1) continue;
            T tr = dfs(e.dest, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0) continue;
            e.flow += tr; e2.flow -= tr;
            return tr;
        }
        return 0;
    }
    T get_flow() {
        T res_flow = 0;
        while (true) {
            if (!bfs()) {
                scaling_lim >>= 1;
                if (!scaling_lim) break;
                else continue;
            }
            while (T pushed = dfs(s, maxT))
                res_flow += pushed;
        }
        return res_flow;
    }
};



void  solve() {
    ll n;

    cin >> n;
	vll a(n);
	vll b(n);

	for (auto& e : a) cin >> e;
    for (auto& e : b) cin >> e;
	
	//vvpll graph (101 + 2);

	ll s = n+3;
	ll t = n+2;
    max_flow<ll> fn(n+5, s, t);
    auto addedge = [&](ll a, ll b, ll w) {
        fn.add_edge(a, b, w);
        //graph[a].push_back({ b, w });
	};

	for (ll i = 0; i < n; i++) {
		if (b[i] > 0) {
			addedge(s, i, b[i]);
		}
		if (b[i] < 0) {
			addedge(i, t, -b[i]);
		}
	}

	vvll ind(105);
	for (ll i = 0; i < n; i++) {
		for (ll j = 1; j < a[i] + 1; j++) {
            if (a[i] % j == 0) {
                if (ind[j].empty()) continue;
                addedge(i, ind[j].back(), 1e16);
            }
		}
		ind[a[i]].push_back(i);
	}

	ll maxsum = 0;
	for (ll i = 0; i < n; i++) {
		if (b[i] > 0) maxsum += b[i];
	}

	ll flow = fn.get_flow();
    cout << maxsum - flow << endl;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll t = 1;
        //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}