#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define setIO(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
#define closefile fclose(stdin), fclose(stdout)
#define m_p make_pair
#define sz(x) (int)x.size()
#define see(x) cerr << x << " "
#define seeln(x) cerr << x << endl
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) {cerr<< #x"[" << l << " ~ " << r << "] = "; for (int _i = l; _i <= r; ++_i) cerr << x[_i] << " "; cerr << endl;}
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define gc() getchar()
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
template <class T> void read(T &x)
{
    x = 0; int c = gc(); int flag = 0;
    while (c < '0' || c > '9') flag |= (c == '-'), c = gc();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) +(c ^ 48), c = gc();
    if (flag) x = -x;
}
template <class T> T _max(T a, T b){return b < a ? a : b;}
template <class T> T _min(T a, T b){return a < b ? a : b;}
template <class T> bool checkmax(T &a, T b){return a < b ? a = b, 1 : 0;}
template <class T> bool checkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
const int N = 3005, inf = 0x3f3f3f3f;
int n, a[N], b[N];
int occ[105];
struct Flow
{
	static const int Node = 3010, Edge = 100000;
	int n, S, T;
	int e = 0, nxt[Edge << 1], point[Edge << 1], value[Edge << 1];
	int first[Node], cur[Node], q[Node], dep[Node];
	void add_edge(int x, int y, int c)
	{
		nxt[++e] = first[x];
		first[x] = e;
		point[e] = y;
		value[e] = c;
	}

	void add_arc(int x, int y, int c)
	{
		//see(x); see(y); seeln(c);
		add_edge(x, y, c);
		add_edge(y, x, 0);
	}

	void init(int m, int s, int t)
	{
		e = 1; n = m; S = s; T = t;
		memset(first, -1, sizeof(int) * (n + 1));
	}

	int bfs(int s, int t)
	{
		memset(dep, 0x3f, sizeof(int) * (n + 1));
		memcpy(cur, first, sizeof(int) * (n + 1));
		int head = 1, tail = 0;
		q[++tail] = s; dep[s] = 0;
		while (head <= tail)
		{
			int x = q[head++];
			for (int i = first[x]; ~i; i = nxt[i])
			{
				int to = point[i];
				if (value[i] && checkmin(dep[to], dep[x] + 1))
				{
					q[++tail] = to;
				}
			}
		}
		if (dep[t] == inf) return 0;
		else return 1;
	}

	int dfs(int now, int flow)
	{
		if (now == T) return flow;
		int used = 0;
		for (int i = cur[now]; ~i; i = nxt[i])
		{
			cur[now] = i;
			int to = point[i];
			if (value[i] && dep[to] == dep[now] + 1)
			{
				int low = dfs(to, _min(flow - used, value[i]));
				if (low)
				{
					used += low;
					value[i] -= low; value[i ^ 1] += low;
					if (used == flow) break;
				}
			}
		}
		return used;
	}

	int maxflow()
	{
		int ans = 0;
		while (bfs(S, T))
		{
			while (1)
			{
				int t = dfs(S, inf);
				if (!t) break;
				ans += t;
			}
		}
		return ans;
	}
}F;

void init()
{
	read(n);
	for (int i = 1; i <= n; ++i) read(a[i]);
	for (int i = 1; i <= n; ++i) read(b[i]);
	F.init(n + 2, n + 1, n + 2);
	for (int i = 1; i <= n; ++i)
	{
		if (b[i] >= 0) F.add_arc(F.S, i, b[i]);
		else F.add_arc(i, F.T, -b[i]);
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= a[i]; ++j) if (a[i] % j == 0 && occ[j])
		{
			F.add_arc(i, occ[j], inf);
		}
		occ[a[i]] = i;
	}
}

void solve()
{
	int cut = F.maxflow(), ans = 0;
	for (int i = 1; i <= n; ++i) if (b[i] >= 0) ans += b[i];
	ans -= cut;
	printf("%d\n", ans);
}

int main()
{
	//setIO("");
	init();
	solve();
	return 0;
}
