#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long a[N], c[N], p[N], x[N];
long long add[4 * N], t[4 * N];
void build(int v, int l, int r) {
  if (r - l == 1) {
    t[v] = c[l];
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v + 1, l, mid);
  build(2 * v + 2, mid, r);
  t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
void push(int v, int l, int r) {
  if (add[v]) {
    if (r - l > 1) {
      int x = 2 * v + 1, y = 2 * v + 2;
      t[x] += add[v];
      t[y] += add[v];
      add[x] += add[v];
      add[y] += add[v];
    }
    add[v] = 0;
  }
}
void upd(int v, int l, int r, int L, int R, int x) {
  if (L >= R) return;
  if (l == L && r == R) {
    add[v] += x;
    t[v] += x;
    push(v, l, r);
    return;
  }
  push(v, l, r);
  int mid = (l + r) / 2;
  upd(2 * v + 1, l, mid, L, min(mid, R), x);
  upd(2 * v + 2, mid, r, max(mid, L), R, x);
  t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
long long get(int v, int l, int r, int L, int R) {
  if (L >= R) return 1e18;
  push(v, l, r);
  if (l == L && r == R) return t[v];
  int mid = (l + r) / 2;
  return min(get(2 * v + 1, l, mid, L, min(R, mid)),
             get(2 * v + 2, mid, r, max(L, mid), R));
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> p[i], p[i]--, x[p[i]] = i;
  for (int i = 0; i < n; i++) cin >> a[i];
  c[0] = a[0];
  for (int i = 1; i < n; i++) c[i] = c[i - 1] + a[i];
  build(0, 0, n);
  long long res = get(0, 0, n, 0, n - 1);
  for (int i = 0; i < n; i++) {
    int pos = x[i];
    upd(0, 0, n, 0, pos, a[pos]);
    upd(0, 0, n, pos, n, -a[pos]);
    res = min(res, get(0, 0, n, 0, n - 1));
  }
  cout << res << endl;
  return 0;
}
