#include <bits/stdc++.h>
using namespace std;
int64_t trr[800009];
int64_t lzy[800009];
void upd(int64_t n, int64_t s, int64_t e, int64_t l, int64_t r, int64_t x) {
  if (s > r || e < l || s > e) return;
  if (l <= s && e <= r) {
    trr[n] += x;
    lzy[n] += x;
    return;
  }
  if (lzy[n]) {
    lzy[n << 1] += lzy[n];
    lzy[n << 1 | 1] += lzy[n];
    trr[n << 1] += lzy[n];
    trr[n << 1 | 1] += lzy[n];
    lzy[n] = 0;
  }
  int64_t m = (s + e) >> 1;
  upd(n << 1, s, m, l, r, x);
  upd(n << 1 | 1, m + 1, e, l, r, x);
  trr[n] = min(trr[n << 1], trr[n << 1 | 1]);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  memset(trr, 0, sizeof(trr));
  memset(lzy, 0, sizeof(lzy));
  int64_t n;
  cin >> n;
  int64_t a[n + 1], b[n + 1], p[n + 1];
  for (int64_t i = 1; i <= n; i++) {
    cin >> p[i];
    b[p[i]] = i;
  }
  for (int64_t i = 1; i <= n; i++) cin >> a[i];
  int64_t s = 0, ans = min(a[1], a[n]);
  for (int64_t i = 1; i <= n; i++) {
    s += a[b[i]];
    upd(1, 1, n, i, i, s);
  }
  for (int64_t i = 1; i < n; i++) {
    upd(1, 1, n, p[i], n, -a[i]);
    upd(1, 1, n, 1, p[i] - 1, a[i]);
    ans = min(ans, trr[1]);
  }
  cout << ans << "\n";
}
