#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
#define d(x) cerr << #x << " = " << x << endl
const int maxn = 3005;
const int maxm = 1000005;
const int inf = 0x3f3f3f3f;
int n;
int a[maxn], b[maxn];
int dis[maxn], cur[maxn], vis[maxn];
int head[maxn], nxt[maxm << 1], tail[maxm << 1], cap[maxm << 1], ecnt = 1;

void addedge(int u, int v, int c) {
	nxt[++ecnt] = head[u];
	head[u] = ecnt;
	tail[ecnt] = v;
	cap[ecnt] = c;
}

int bfs(int S, int T) {
	memset(dis, 0, sizeof(dis));
	queue<int> que; que.push(S); dis[S] = 1;
	while (!que.empty()) {
		int u = que.front(); que.pop();
		for (int e = head[u]; e; e = nxt[e]) {
			int v = tail[e];
			if (cap[e] && !dis[v]) {
				dis[v] = dis[u] + 1;
				que.push(v);
			}
		}
	}
	return (dis[T] > 0);
}

int dfs(int u, int T, int flowin) {
	if (u == T || !flowin) return flowin;
	int maxflow = 0;
	for (int &e = cur[u]; e; e = nxt[e]) {
		int v = tail[e];
		if (cap[e] && dis[v] == dis[u] + 1) {
			int tmp = dfs(v, T, min(flowin, cap[e]));
			maxflow += tmp; flowin -= tmp;
			cap[e] -= tmp; cap[e ^ 1] += tmp;
			if (!flowin) break;
		}
	}
	if (!maxflow) dis[u] = 0;
	return maxflow;
}

int dinic(int S, int T) {
	int ret = 0;
	while (bfs(S, T)) {
		memcpy(cur, head, sizeof(head));
		ret += dfs(S, T, inf);
	}
	return ret;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
	int S = n + 1, T = n + 2, sum = 0;
	for (int i = 1; i <= n; i++) {
		if (b[i] >= 0) {
			addedge(S, i, b[i]); addedge(i, S, 0);
			sum += b[i];
		} else {
			addedge(i, T, -b[i]); addedge(T, i, 0);
		}
	}
	for (int i = 1; i <= n; i++) {
		memset(vis, 0, sizeof(vis));
		for (int j = i - 1; j >= 1; j--) {
			if (a[i] % a[j] != 0) continue;
			if (!vis[a[j]]) {
				addedge(i, j, inf); addedge(j, i, 0);
				for (int p = 1; p * p <= a[j]; p++) {
					if (a[j] % p != 0) continue;
					int p1 = p, p2 = a[j] / p;
					vis[p1] = vis[p2] = 1;
				}
			}
		}
	}
	printf("%d\n", sum - dinic(S, T));
    return 0;
}