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

#define MP make_pair
#define FF first
#define SS second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll MOD = 1e9 + 7;
const int INF = 2e9;
const ll LINF = 4e18;
const ll delta = 24659;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 3e3 + 5;
int n, a[N];

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

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

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

	void setST(int s, int t) {
		this->s = s;
		this->t = t;
	}

    void addEdge(int v, int u, int 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;
    }

    int dfs(int v, int 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;
            int 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;
    }

    int flow() {
        int 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 (int pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        return f;
    }
};

Dinic maxflow(N);

int ans = 0;

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	maxflow.setST(0, n + 1);
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
		for (int j = i - 1; j >= 1; j--)
			if (a[i] % a[j] == 0) {
				bool found = 0;
				for (int k = j + 1; k < i; k++)
					if (a[k] == a[j]) {
						found = 1;
						break;
					}
				if (!found)
					maxflow.addEdge(i, j, INF);
			}
	for (int i = 1; i <= n; i++)
		cin >> a[i], ans += max(0, a[i]);
	for (int i = 1; i <= n; i++) {
		if (a[i] < 0)
			maxflow.addEdge(i, n + 1, -a[i]);
		if (a[i] > 0)
			maxflow.addEdge(0, i, a[i]);
	}
	cout << ans - maxflow.flow() << endl;
}



















