#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long md = 1000000007) {
  long long res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1) res = (res * x) % md;
    x = (x * x) % md;
    y = y >> 1;
  }
  return res % md;
}
long long seg[800001];
long long lazy[800001];
void lazyprop(long long index, long long ss, long long se) {
  if (lazy[index]) {
    if (ss != se) {
      lazy[index * 2 + 1] += lazy[index];
      lazy[index * 2 + 2] += lazy[index];
    }
    seg[index] += lazy[index];
    lazy[index] = 0;
  }
}
long long query(long long index, long long ss, long long se, long long qs,
                long long qe) {
  if (qs > se || qe < ss) return 1000000000000000000LL;
  lazyprop(index, ss, se);
  if (qs <= ss && se <= qe) {
    return seg[index];
  }
  long long mid = (ss + se) / 2;
  return min(query(index * 2 + 1, ss, mid, qs, qe),
             query(index * 2 + 2, mid + 1, se, qs, qe));
}
void update(long long index, long long ss, long long se, long long qs,
            long long qe, long long val) {
  if (qs > se || qe < ss) {
    lazyprop(index, ss, se);
    return;
  }
  if (qs <= ss && se <= qe) {
    lazy[index] += val;
    lazyprop(index, ss, se);
    return;
  }
  lazyprop(index, ss, se);
  long long mid = (ss + se) / 2;
  update(index * 2 + 1, ss, mid, qs, qe, val);
  update(index * 2 + 2, mid + 1, se, qs, qe, val);
  seg[index] = min(seg[2 * index + 1], seg[2 * index + 2]);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  vector<long long> p(n), a(n);
  for (long long i = 0; i < n; i++) cin >> p[i], p[i]--;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) {
    update(0, 0, n - 1, p[i], n - 1, a[i]);
  }
  long long ans = a[0];
  long long index = n - 1;
  long long second = 0;
  for (long long i = 0; i < n - 1; i++) {
    second += a[i];
    update(0, 0, n - 1, p[i], n - 1, -2 * a[i]);
    ans = min(ans, second + query(0, 0, n - 1, 0, n - 1));
  }
  cout << ans;
}
