#include <bits/stdc++.h>
using namespace std;
long long n;
long long p[200005], q[200005], a[200005], s[200005];
struct T {
  long long l, r, mid;
  long long mn, tag;
} t[200005 << 2];
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
    if (ch == -1) return 0;
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void pushup(long long rt) {
  t[rt].mn = min(t[(rt << 1)].mn, t[(rt << 1 | 1)].mn);
}
inline void pushdown(long long rt) {
  if (t[rt].tag)
    t[(rt << 1)].tag += t[rt].tag, t[(rt << 1 | 1)].tag += t[rt].tag,
        t[(rt << 1)].mn += t[rt].tag, t[(rt << 1 | 1)].mn += t[rt].tag,
        t[rt].tag = 0;
}
void build(long long rt, long long l, long long r) {
  long long mid = (l + r) >> 1;
  t[rt].l = l, t[rt].r = r, t[rt].mid = mid;
  if (l == r) {
    t[rt].mn = s[l];
    return;
  }
  build((rt << 1), l, mid), build((rt << 1 | 1), mid + 1, r), pushup(rt);
}
void update(long long rt, long long l, long long r, long long x) {
  if (l <= t[rt].l && t[rt].r <= r) {
    t[rt].mn += x, t[rt].tag += x;
    return;
  }
  pushdown(rt);
  if (l <= t[rt].mid) update((rt << 1), l, r, x);
  if (t[rt].mid < r) update((rt << 1 | 1), l, r, x);
  pushup(rt);
}
long long ans;
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) p[i] = read(), q[p[i]] = i;
  for (long long i = 1; i <= n; i++) a[i] = read(), s[i] = s[i - 1] + a[i];
  build(1, 1, n - 1), ans = t[1].mn;
  for (long long i = 1; i <= n; i++) {
    if (q[i] - 1 >= 1) update(1, 1, q[i] - 1, a[q[i]]);
    if (q[i] <= n - 1) update(1, q[i], n - 1, -a[q[i]]);
    ans = min(ans, t[1].mn);
  }
  cout << ans << endl;
  return 0;
}
