#include <bits/stdc++.h>
using namespace std;
vector<long long> tree(800004), lazy(800004);
void init(int st, int en, int nd, long long arr[]) {
  lazy[nd] = 0;
  if (st == en) {
    tree[nd] = arr[st];
    return;
  }
  int md = (st + en) / 2;
  init(st, md, 2 * nd, arr);
  init(md + 1, en, 2 * nd + 1, arr);
  tree[nd] = min(tree[2 * nd], tree[2 * nd + 1]);
}
void update(int st, int en, int nd, int l, int r, long long df) {
  if (st > en) return;
  if (lazy[nd] != 0) {
    tree[nd] += lazy[nd];
    if (st != en) {
      lazy[2 * nd] += lazy[nd];
      lazy[2 * nd + 1] += lazy[nd];
    }
    lazy[nd] = 0;
  }
  if (en < l || r < st) return;
  if (l <= st && en <= r) {
    tree[nd] += df;
    if (st != en) {
      lazy[2 * nd] += df;
      lazy[2 * nd + 1] += df;
    }
    return;
  }
  int md = (st + en) / 2;
  update(st, md, 2 * nd, l, r, df);
  update(md + 1, en, 2 * nd + 1, l, r, df);
  tree[nd] = min(tree[2 * nd], tree[2 * nd + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    long long arr[n], pz[n], sm[n];
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      arr[x - 1] = i;
    }
    for (int i = 0; i < n; i++) cin >> pz[i];
    sm[0] = pz[0];
    n--;
    for (int i = 1; i < n; i++) sm[i] = pz[i] + sm[i - 1];
    init(0, n - 1, 1, sm);
    long long ans = sm[0];
    for (int i = 0; i < n + 1; i++) {
      int ps = arr[i];
      long long up = pz[ps];
      update(0, n - 1, 1, ps, n - 1, -up);
      update(0, n - 1, 1, 0, ps - 1, up);
      ans = min(ans, tree[1]);
    }
    cout << ans << endl;
  }
  return 0;
}
