#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

enum { maxn = 3002 };

struct edge {
  int v, cap;
};

int a[maxn], b[maxn];
vector<edge> peer[maxn];
int parent[maxn];
edge *parentedge[maxn];
int flow[maxn];
int q[maxn], qb, qe;

void addedge(int u, int v, int cap) {
  peer[u].push_back({v, cap});
  peer[v].push_back({u, 0});
}

int main() {
  int n;
  scanf("%d", &n);
  int inf = 1234567890;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  int last[101] = {};
  for (int i = 1; i <= n; i++) {
    if (b[i] > 0) addedge(0, i, b[i]);
    if (b[i] < 0) addedge(i, n + 1, -b[i]);
    for (int j = 1; j <= a[i]; j++) {
      if (last[j] == 0 || a[i] % j != 0) continue;
      addedge(i, last[j], inf);
    }
    last[a[i]] = i;
  }

  // find max flow.
  int totalflow = 0;
  flow[0] = inf;
  while (true) {
    // find a path from 0 to n + 1.
    memset(parent, -1, sizeof(int) * (n + 2));
    parent[0] = 0;
    qb = 0;
    qe = 1;
    while (qb != qe) {
      int curnode = q[qb++];
      for (size_t i = 0; i < peer[curnode].size(); i++) {
        edge e = peer[curnode][i];
        if (e.cap == 0) continue;
        if (parent[e.v] != -1) continue;
        parent[e.v] = curnode;
        parentedge[e.v] = &peer[curnode][i];
        flow[e.v] = min(flow[curnode], e.cap);
        q[qe++] = e.v;
      }
    }
    if (parent[n + 1] == -1) break;

    // augment the path.
    int f = flow[n + 1];
    totalflow += f;
    int u = n + 1;
    while (u != 0) {
      int p = parent[u];
      parentedge[u]->cap -= f;
      for (size_t i = 0; i < peer[u].size(); i++) {
        if (peer[u][i].v != p) continue;
        peer[u][i].cap += f;
        break;
      }
      u = p;
    }
  }

  // calculate and print the cost.
  int sum = 0;
  for (int i = 1; i <= n; i++) sum += max(0, b[i]);
  printf("%d\n", sum - totalflow);
  return 0;
}
