#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for(int i = (int) a; i <= (int) b; i ++)
#define per(i, a, b) for(int i = (int) a; i >= (int) b; i --)
using namespace std;
const int maxn = 3010, inf = 1e9 + 7;
int n, s, t, a[maxn], b[maxn], dep[maxn], head[maxn], tot = 1, cur[maxn], sum;
bool mp[maxn][maxn];
struct line { int to, nxt, cap; } edge[maxn * 40];
void add(int u, int v, int c) {
	edge[++ tot].to = v, edge[tot].cap = c, edge[tot].nxt = head[u], head[u] = tot;
	edge[++ tot].to = u, edge[tot].cap = 0, edge[tot].nxt = head[v], head[v] = tot;
}
bool bfs() {
	memset(dep, 0, sizeof(dep));
	memcpy(cur, head, sizeof(cur));
	queue < int > q; q.push(s), dep[s] = 1;
	while(!q.empty()) {
		int u = q.front(); q.pop(); 
		for(int i = head[u]; i; i = edge[i].nxt) if(edge[i].cap && !dep[edge[i].to]) {
			int v = edge[i].to;
			dep[v] = dep[u] + 1, q.push(v);
		}
	}
	return dep[t];
} 
int dfs(int u, int flow) {
	if(u == t || flow == 0) return flow;
	int res = 0;
	for(int i = cur[u]; i; i = edge[i].nxt) {
		cur[u] = i;
		int v = edge[i].to;
		if(!edge[i].cap || dep[v] != dep[u] + 1) continue;
		int k = dfs(v, min(flow - res, edge[i].cap));
		edge[i].cap -= k, edge[i ^ 1].cap += k, res += k;
	}
	return res;
}
int dinic() {
	int res = 0;
	while(bfs()) res += dfs(s, inf);
	return res;
}
int main()
{
	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", &a[i]);
	rep(i, 1, n) scanf("%d", &b[i]);
	s = 0, t = n + 1;
	rep(i, 1, n) if(b[i] > 0) add(s, i, b[i]), sum += b[i]; else add(i, t, -b[i]);
	rep(i, 1, n) per(j, i - 1, 1) if(a[i] % a[j] == 0 && !mp[i][j]) {
		add(i, j, inf);
		mp[i][j] = 1;
		rep(k, 1, j - 1) mp[i][k] |= mp[j][k];
	}
	printf("%d\n", sum - dinic());
	return 0;
}