#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define FOR(i,n,m) for(int i=n; i<(int)m; i++)
#define ROF(i,n,m) for(int i=n; i>(int)m; i--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define ri(a) scanf("%d",&a)
#define rii(a,b) ri(a),ri(b)
#define riii(a,b,c) rii(a,b),ri(c)
#define lri(a) scanf("%lld",&a)
#define lrii(a,b) lri(a),lri(b)
#define ms(obj,val) memset(obj,val,sizeof(obj))
#define all(x) x.begin(),x.end()
#define debug true
#define dprintf debug && printf

const int INF = 0x3f3f3f3f;
const ll INFLL = 1e18;
const int MOD = 1e9+7;
const int MAXN = 3e3+5;

struct edge {
	int a, b;
	ll cap, flow;
};
 
int N, M, s, t, d[MAXN], ptr[MAXN];
vector<edge> e;
vi G[MAXN];
 
void add_edge(int a, int b, ll cap) {
	edge e1 = { a, b, cap, 0 };
	edge e2 = { b, a, 0, 0 };
	G[a].pb((int)e.size());
	e.pb(e1);
	G[b].pb((int) e.size());
	e.pb(e2);
}
 
bool bfs() {
	queue<int> q; q.push(s);
	memset (d, -1, (N+1) * sizeof d[0]);
	d[s] = 0;
	while(!q.empty() && d[t] == -1) {
		int v = q.front(); q.pop();
		FOR(i,0,G[v].size()){
			int id = G[v][i], u = e[id].b;
			if(d[u] == -1 && e[id].flow < e[id].cap){
				q.push(u);
				d[u] = d[v] + 1;
			}
		}
	}
	return d[t] != -1;
}
 
ll dfs(int v, ll flow){
	if(!flow) return 0;
	if (v == t)  return flow;
	for (; ptr[v]<(int)G[v].size(); ++ptr[v]) {
		int id = G[v][ptr[v]], u = e[id].b;
		if(d[u] != d[v]+1) continue;
		int pushed = dfs(u, min(flow, (ll) e[id].cap - e[id].flow));
		if(pushed){
			e[id].flow += pushed;
			e[id^1].flow -= pushed;
			return pushed;
		}
	}
	return 0;
}
 
ll dinic() {
	ll flow = 0;
	while(true){
		if (!bfs())  break;
		memset (ptr, 0, (N+1) * sizeof ptr[0]);
		while (ll pushed = dfs(s, INF))
			flow += pushed;
	}
	return flow;
}

int a[MAXN], b[MAXN];
int lst[111];
int n;

int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);

    ms(lst, -1);
    ri(n);
    FOR(i,0,n) ri(a[i]);
    FOR(i,0,n) ri(b[i]);

    N = n;
    s = N++;
    t = N++;

    FOR(i,0,n) 
        if(b[i] < 0) add_edge(i, t, -b[i]);
        else add_edge(s, i, b[i]);

    FOR(i,0,n){
        if(lst[a[i]] != -1) add_edge(i ,lst[a[i]], INF);
        lst[a[i]] = i;
        ROF(j,i-1,-1) if(a[i] % a[j] == 0 && j == lst[a[j]]){
            add_edge(i, j, INF);
        }
    }
    
    int ans = 0;
    FOR(i,0,n) ans += max(b[i], 0);
    ans -= dinic();
    printf("%d\n", ans);


    return 0;
}
