#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 3007;
const int M = 200 * N;
queue<int>q;
int head[N], nxt[M], to[M], vu[M], edge, n = N, s, t;
void addedge(int f, int t, int v)
{
    edge++; nxt[edge] = head[f];head[f] = edge;
    to[edge] = t; vu[edge] = v;
    edge++; nxt[edge] = head[t]; head[t] = edge;
    to[edge] = f; vu[edge] = 0;
    return;
}
int dis[N];
bool bfs()
{
    memset(dis, -1, sizeof(int) * n);
    dis[s] = 0; q.push(s);
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (int e = head[u]; e != -1; e = nxt[e])
        {
            int t = to[e];
            if (dis[t] == -1 && vu[e])
            {
                dis[t] = dis[u] + 1;
                q.push(t);
            }
        }
    }
    return dis[t] != -1;
}
int cur[N];
int dfs(int u, int flow)
{
    if (u == t)return flow;
    int delta = flow;
    for (int e = cur[u]; e != -1; cur[u] = e = nxt[e])
    {
        int t = to[e];
        if (dis[t] == dis[u] + 1 && vu[e])
        {
            int s = dfs(t, min(vu[e], delta));
            vu[e] -= s;
            vu[e ^ 1] += s;
            delta -= s;
        }
    }
    if (delta == flow)dis[u] = -1;
    return flow - delta;
}
int dinic()
{
    int res = 0;
    while (bfs())
    {
        for (int i = 0; i < n; i++)cur[i] = head[i];
        res += dfs(s, 0x3f3f3f3f);
    }
    return res;
}
int a[N], b[N], k;
bool vis[107];
void build()
{
    for (int i = 0; i < n; i++)
    {
        memset(vis, 0, sizeof(vis));
        if (b[i] > 0) { addedge(s, i, b[i]); k += b[i]; }
        else if (b[i] < 0)addedge(i, t, -b[i]);
        for (int j = i - 1; j >= 0 && !vis[a[i]]; j--)
        {
            if (a[i] % a[j] == 0 && !vis[a[j]])
            {
                addedge(i, j, 0x3f3f3f3f);
                vis[a[j]] = true;
            }
        }
    }
    n += 2;
    return;
}
void init()
{
    memset(head, -1, sizeof(head)); edge = -1;
    s = n; t = n + 1;
}
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)scanf("%d", a + i);
    for (int i = 0; i < n; i++)scanf("%d", b + i);
    init(); build();
    cout << k - dinic() << endl;
}