#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e18;
const long long int MOD = (long long int)1e9 + 7;
const long long int maxn = (long long int)2 * 1e5 + 10, L = 23, MX = 1e4;
struct vertex {
  long long int val, add;
};
vertex tree[4 * maxn];
long long int pref[maxn];
void push(long long int v, long long int vl, long long int vr) {
  if (!tree[v].add) return;
  long long int t = tree[v].add;
  tree[v].val += t;
  if (vl + 1 != vr) {
    tree[2 * v].add += t;
    tree[2 * v + 1].add += t;
  }
  tree[v].add = 0;
}
void build(long long int v, long long int vl, long long int vr) {
  if (vl >= vr) return;
  if (vl + 1 == vr)
    tree[v].val = pref[vl];
  else {
    long long int mid = (vl + vr) >> 1;
    build(2 * v, vl, mid);
    build(2 * v + 1, mid, vr);
    tree[v].val = min(tree[2 * v].val, tree[2 * v + 1].val);
  }
}
long long int get(long long int v, long long int vl, long long int vr,
                  long long int l, long long int r) {
  push(v, vl, vr);
  if (vr <= l || r <= vl) return INF;
  if (l <= vl && vr <= r) return tree[v].val;
  long long int mid = (vl + vr) >> 1;
  long long int x = get(2 * v, vl, mid, l, r),
                y = get(2 * v + 1, mid, vr, l, r);
  return min(x, y);
}
void upd(long long int v, long long int vl, long long int vr, long long int l,
         long long int r, long long int x) {
  push(v, vl, vr);
  if (vr <= l || r <= vl) return;
  if (l <= vl && vr <= r) {
    tree[v].add += x;
    push(v, vl, vr);
    return;
  }
  long long int mid = (vl + vr) >> 1;
  upd(2 * v, vl, mid, l, r, x);
  upd(2 * v + 1, mid, vr, l, r, x);
  tree[v].val = min(tree[2 * v].val, tree[2 * v + 1].val);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  vector<long long int> a(n), wh(n);
  for (long long int i = 0; i < n; ++i) {
    long long int x;
    cin >> x;
    wh[x - 1] = i;
  }
  for (long long int i = 0; i < n; ++i) cin >> a[i];
  pref[0] = a[0];
  for (long long int i = 1; i < n; ++i) {
    pref[i] = pref[i - 1] + a[i];
  }
  build(1, 0, n);
  long long int ans = get(1, 0, n, 0, n - 1);
  for (long long int i = 0; i < n; ++i) {
    long long int now = wh[i];
    upd(1, 0, n, 0, now, a[now]);
    upd(1, 0, n, now, n, -a[now]);
    ans = min(ans, get(1, 0, n, 0, n - 1));
  }
  cout << ans;
  return 0;
}
