#include <bits/stdc++.h>
using namespace std;
struct Tree {
  long long l, r, sum, qmin;
} node[2000005];
long long f[200005], a[200005], b[200005];
void BT(long long rt, long long l, long long r) {
  node[rt].l = l;
  node[rt].r = r;
  if (l == r) {
    f[l] = rt;
    node[rt].sum = node[rt].qmin = b[l];
    return;
  }
  BT(rt * 2, l, (l + r) / 2);
  BT(rt * 2 + 1, (l + r) / 2 + 1, r);
  node[rt].sum = node[rt * 2].sum + node[rt * 2 + 1].sum;
  node[rt].qmin =
      min(node[rt * 2].qmin, node[rt * 2].sum + node[rt * 2 + 1].qmin);
}
void gx(long long rt, long long a, long long b) {
  if (node[rt].l == node[rt].r) {
    node[rt].sum = b;
    node[rt].qmin = b;
    return;
  }
  if (a <= (node[rt].l + node[rt].r) / 2) {
    gx(rt * 2, a, b);
  } else {
    gx(rt * 2 + 1, a, b);
  }
  node[rt].sum = node[rt * 2].sum + node[rt * 2 + 1].sum;
  node[rt].qmin =
      min(node[rt * 2].qmin, node[rt * 2].sum + node[rt * 2 + 1].qmin);
}
long long cx(long long rt) { return node[rt].qmin; }
int main() {
  int n, x;
  scanf("%d", &n);
  long long qSum = 0, ans = 1ll << 60;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    a[x] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
  }
  BT(1, 1, n - 1);
  for (int i = 0; i <= n; i++) {
    qSum += b[a[i]];
    if (a[i] < n && a[i] > 0) gx(1, a[i], -b[a[i]]);
    ans = min(ans, cx(1) + qSum);
  }
  printf("%lld\n", ans);
  return 0;
}
