#include <bits/stdc++.h>
using namespace std;
long long segtree[1000000], lazy[1000000];
void build(int x, vector<long long> &a, int l, int r) {
  if (l > r) return;
  if (l == r) {
    segtree[x] = a[l];
    lazy[x] = 0;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * x, a, l, mid);
  build((2 * x) + 1, a, mid + 1, r);
  segtree[x] = min(segtree[2 * x], segtree[(2 * x) + 1]);
  lazy[x] = 0;
  return;
}
void propagate(int x, int l, int r) {
  if (l >= r) return;
  segtree[x] += lazy[x];
  lazy[2 * x] += lazy[x];
  lazy[(2 * x) + 1] += lazy[x];
  lazy[x] = 0;
  return;
}
void update(int x, int l, int r, int lx, int rx, long long num) {
  if ((lx > rx) || (l > r)) return;
  if ((l == lx) && (r == rx)) {
    lazy[x] += num;
    return;
  }
  propagate(x, l, r);
  int mid = (l + r) / 2;
  update(2 * x, l, mid, lx, min(rx, mid), num);
  update((2 * x) + 1, mid + 1, r, max(lx, mid + 1), rx, num);
  segtree[x] = min(segtree[2 * x] + lazy[2 * x],
                   segtree[(2 * x) + 1] + lazy[(2 * x) + 1]);
  return;
}
long long query(int x, int l, int r, int lx, int rx) {
  if ((lx > rx) || (l > r)) return 1000000000000000000;
  if ((l == lx) && (r == rx)) return (segtree[x] + lazy[x]);
  propagate(x, l, r);
  int mid = (l + r) / 2;
  return min(query(2 * x, l, mid, lx, min(rx, mid)),
             query((2 * x) + 1, mid + 1, r, max(lx, mid + 1), rx));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> per(n), pos(n + 1);
  vector<long long> a1(n), acum(n);
  for (int i = 0; i < n; i++) {
    cin >> per[i];
    pos[per[i]] = i;
  }
  for (int i = 0; i < n; i++) cin >> a1[i];
  acum[0] = a1[0];
  for (int i = 1; i < n; i++) acum[i] = acum[i - 1] + a1[i];
  build(1, acum, 0, n - 1);
  long long ans = query(1, 0, n - 1, 0, n - 2);
  for (int i = 1; i <= n; i++) {
    long long pn = pos[i];
    update(1, 0, n - 1, 0, pn - 1, a1[pn]);
    update(1, 0, n - 1, pn, n - 1, -a1[pn]);
    ans = min(ans, query(1, 0, n - 1, 0, n - 2));
  }
  cout << ans << "\n";
  return 0;
}
