#include <bits/stdc++.h>
const long long maxn = 3e6 + 9;
long long Read() {
  long long x(0), f(1);
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3ll) + (x << 1ll) + c - '0';
    c = getchar();
  }
  return x * f;
}
long long T, n;
long long a[maxn], val[maxn];
struct SQT {
  long long lazy[maxn], mi[maxn];
  void pd(long long nw) {
    if (lazy[nw]) {
      lazy[nw << 1] += lazy[nw];
      lazy[nw << 1 | 1] += lazy[nw];
      mi[nw << 1] += lazy[nw];
      mi[nw << 1 | 1] += lazy[nw];
      lazy[nw] = 0;
    }
  }
  void Modify(long long nw, long long l, long long r, long long lt,
              long long rt, long long v) {
    if (lt > rt) return;
    if (lt <= l && rt >= r) {
      lazy[nw] += v;
      mi[nw] += v;
      return;
    }
    pd(nw);
    long long mid(l + r >> 1);
    if (lt <= mid) Modify(nw << 1, l, mid, lt, rt, v);
    if (rt > mid) Modify(nw << 1 | 1, mid + 1, r, lt, rt, v);
    mi[nw] = std::min(mi[nw << 1], mi[nw << 1 | 1]);
  }
  long long Queryi(long long nw, long long l, long long r, long long lt,
                   long long rt) {
    if (lt > rt) return 1e18;
    if (lt <= l && rt >= r) return mi[nw];
    pd(nw);
    long long mid(l + r >> 1), ret(1e18);
    if (lt <= mid) ret = std::min(ret, Queryi(nw << 1, l, mid, lt, rt));
    if (rt > mid) ret = std::min(ret, Queryi(nw << 1 | 1, mid + 1, r, lt, rt));
    return ret;
  }
  void Init() {
    memset(lazy, 0, sizeof(lazy));
    memset(mi, 0, sizeof(mi));
  }
} T3;
long long pos[maxn];
int main() {
  T3.Init();
  n = Read();
  for (long long i = 1; i <= n; ++i) a[i] = Read(), pos[a[i]] = i;
  for (long long i = 1; i <= n; ++i) val[i] = Read();
  long long ans(val[1]);
  for (long long i = 1; i <= n; ++i) {
    long long x(pos[i]);
    T3.Modify(1, 1, n, x, n, val[x]);
  }
  for (long long i = 1; i <= n; ++i) {
    long long x(pos[i]), ret(0);
    T3.Modify(1, 1, n, x, n, -val[x]);
    T3.Modify(1, 1, n, 1, x - 1, val[x]);
    if (x == n) continue;
    ret = T3.Queryi(1, 1, n, x, n - 1);
    ans = std::min(ans, ret);
  }
  printf("%lld\n", ans);
}
