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

const long long INF = 1e9 + 7;

struct vl {
	size_t operator()(const pair<int, int>& a) const {
		return hash<int>()((a.first << 15) ^ (a.second));
	}
};

int n, s, t, lst[105], a[3002], b[3002];
int to[3002];
unordered_map<pair<int, int>, int, vl> cap, cur;
vector<vector<int>> g;
int positive_flow = 0, max_flow = 0;

inline void add_edge(int u, int v, long long w) {
	cap[{u, v}] = w;
	g[u].push_back(v);
	g[v].push_back(u);
}

void bfs_flow() {
	queue<int> que;
	que.push(s);
	while (not que.empty() && to[t] == -1) {
		auto p = que.front();
		que.pop();
		for (auto &c : g[p]) {
			if (to[c] == -1 && cap[{p, c}] > cur[{p, c}]) {
				to[c] = p;
				que.push(c);
			}
		}
	}
}

bool find_path() {
	fill(to, to + 3002, -1);
	to[s] = -2;
	bfs_flow();
	return to[t] != -1;
}

void inc_flow() {
	int delta = INF;
	int v = t;
	while (v != s) {
		int u = to[v];
		// u -> v
		delta = min(delta, cap[{u, v}] - cur[{u, v}]);
		v = u;
	}
	v = t;
	while (v != s) {
		int u = to[v];
		cur[{u, v}] += delta;
		cur[{v, u}] -= delta;
		v = u;
	}
	max_flow += delta;
}

void solve() {
	cin >> n;
	g.resize(n + 2);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= n; ++i) cin >> b[i];

	s = 0, t = n + 1;

	for (int i = 1; i <= n; ++i) {
		if (b[i] > 0) {
			positive_flow += b[i];
			add_edge(s, i, b[i]);
		}
		else {
			add_edge(i, t, -b[i]);
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int d = 1; d <= a[i]; ++d) {
			if (a[i] % d == 0) {
				if (lst[d] != 0) add_edge(i, lst[d], INF);
			}
		}
		lst[a[i]] = i;
	}
	while (find_path()) inc_flow();

	cout << positive_flow - max_flow;
}

#undef int
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	solve();
	return 0;
}