#include <bits/stdc++.h>
using namespace std;
long long mint[800005], add[800005];
inline void push_up(int rt) {
  mint[rt] = min(mint[rt << 1], mint[rt << 1 | 1]);
}
inline void push_down(int rt, int l, int r) {
  if (add[rt]) {
    mint[rt << 1] += add[rt];
    add[rt << 1] += add[rt];
    mint[rt << 1 | 1] += add[rt];
    add[rt << 1 | 1] += add[rt];
    add[rt] = 0;
  }
}
void build(int rt, int l, int r) {
  if (l == r) {
    mint[rt] = add[rt] = 0;
    return;
  }
  int m = (l + r) >> 1;
  build(rt << 1, l, m);
  build(rt << 1 | 1, m + 1, r);
  push_up(rt);
}
void update(int rt, int l, int r, int L, int R, int v) {
  if (L <= l && r <= R) {
    mint[rt] += v;
    add[rt] += v;
    return;
  }
  push_down(rt, l, r);
  int m = (l + r) >> 1;
  if (L <= m) update(rt << 1, l, m, L, R, v);
  if (m < R) update(rt << 1 | 1, m + 1, r, L, R, v);
  push_up(rt);
}
int n, a[200005], b[200005], fb[200005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    fb[b[i]] = i;
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  build(1, 1, n - 1);
  for (int i = 1; i < n; i++) update(1, 1, n - 1, i, n - 1, a[i]);
  long long ans = mint[1];
  for (int i = 1; i <= n; i++) {
    if (fb[i] != n) {
      update(1, 1, n - 1, fb[i], n - 1, -a[fb[i]]);
    }
    if (fb[i] != 1) {
      update(1, 1, n - 1, 1, fb[i] - 1, a[fb[i]]);
    }
    ans = min(ans, mint[1]);
  }
  printf("%I64d", ans);
}
