#include <bits/stdc++.h>
using namespace std;
int n, sl, fh, p[1000010], a[1000010];
long long ans, tr[1000010], tg[1000010];
int rd() {
  sl = 0;
  fh = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
  return sl * fh;
}
void upd(int x, long long v) {
  tr[x] += v;
  tg[x] += v;
}
void pushdown(int x) {
  if (tg[x]) {
    upd(x << 1, tg[x]);
    upd(x << 1 | 1, tg[x]);
    tg[x] = 0;
  }
}
void update(int x, int l, int r, int L, int R, int v) {
  if (L > R || l > r || L > r || l > R) return;
  if (L <= l && r <= R) return upd(x, v);
  pushdown(x);
  if (L <= ((l + r) >> 1)) update(x << 1, l, ((l + r) >> 1), L, R, v);
  if (R > ((l + r) >> 1)) update(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, v);
  tr[x] = min(tr[x << 1], tr[x << 1 | 1]);
}
int main() {
  n = rd();
  ans = 1e18;
  for (int i = 1; i <= n; ++i) p[i] = rd();
  for (int i = 1; i <= n; ++i)
    a[i] = rd(), update(1, 1, n + 1, p[i] + 1, n + 1, a[i]);
  for (int i = 1; i < n; ++i) {
    update(1, 1, n + 1, p[i] + 1, n + 1, -a[i]);
    update(1, 1, n + 1, 1, p[i], a[i]);
    ans = min(ans, tr[1]);
  }
  printf("%lld\n", ans);
  return 0;
}
