#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
long long qpow(long long a, long long b) {
  long long ans = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
long long gcd(long long a, long long b) { return b > 0 ? gcd(b, a % b) : a; }
int n, m, T;
int a[maxn];
int p[maxn];
long long c[maxn];
long long tr[maxn << 2], lz[maxn << 2];
void build(int l, int r, int rt) {
  lz[rt] = 0;
  if (l == r) {
    tr[rt] = c[l];
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  tr[rt] = min(tr[rt << 1], tr[rt << 1 | 1]);
}
void pushdown(int rt) {
  if (lz[rt]) {
    lz[rt << 1] += lz[rt];
    lz[rt << 1 | 1] += lz[rt];
    tr[rt << 1] += lz[rt];
    tr[rt << 1 | 1] += lz[rt];
    lz[rt] = 0;
  }
}
void update(long long C, int L, int R, int l, int r, int rt) {
  if (l > R || r < L) return;
  if (L <= l && r <= R) {
    tr[rt] += C;
    lz[rt] += C;
    return;
  }
  int mid = l + r >> 1;
  pushdown(rt);
  update(C, L, R, l, mid, rt << 1);
  update(C, L, R, mid + 1, r, rt << 1 | 1);
  tr[rt] = min(tr[rt << 1], tr[rt << 1 | 1]);
}
long long query(int L, int R, int l, int r, int rt) {
  if (l > R || r < L) return 1e18;
  if (L <= l && r <= R) {
    return tr[rt];
  }
  int mid = l + r >> 1;
  pushdown(rt);
  return min(query(L, R, l, mid, rt << 1),
             query(L, R, mid + 1, r, rt << 1 | 1));
}
int pos[maxn];
void de() {
  for (int i = 1; i <= n; i++) {
    cout << query(i, i, 1, n, 1) << " ";
  }
  cout << "\n";
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    c[i] = 1ll * c[i - 1] + a[i];
  }
  build(1, n, 1);
  long long ans = tr[1];
  for (int i = 1; i <= n; i++) {
    if (pos[i] > 1) update(a[pos[i]], 1, pos[i] - 1, 1, n, 1);
    update(-a[pos[i]], pos[i], n, 1, n, 1);
    ans = min(ans, query(1, n - 1, 1, n, 1));
  }
  for (int i = n; i >= 1; i--) {
    c[i] = 1ll * c[i + 1] + a[i];
    ans = min(ans, c[i]);
  }
  cout << ans << endl;
  return 0;
}
