#include <bits/stdc++.h>
using namespace std;
long long digni(long long x, long long y) {
  if (y == 0) return 1;
  if (y % 2 == 0) {
    long long res = digni(x, y / 2);
    return (res * res) % 1000000007;
  }
  return (digni(x, y - 1) * x) % 1000000007;
}
void mod_add(long long &a, long long b) {
  a += b;
  a %= 1000000007;
}
void mod_mul(long long &a, long long b) {
  a *= b;
  a %= 1000000007;
}
long long fak[300000 + 5];
long long invfak[300000 + 5];
void precalc() {
  fak[0] = 1;
  invfak[0] = 1;
  for (long long i = 1; i <= 300000; i++) {
    fak[i] = fak[i - 1] * i;
    fak[i] %= 1000000007;
    invfak[i] = digni(fak[i], 1000000007 - 2);
  }
}
long long bin_c(int x, int y) {
  if (x < y) return 0ll;
  long long sol = fak[x];
  sol *= invfak[y];
  sol %= 1000000007;
  sol *= invfak[x - y];
  sol %= 1000000007;
  return sol;
}
int n;
int poz[300000 + 5];
int p[300000 + 5];
int a[300000 + 5];
long long res[300000 + 5];
long long lazy[4 * 300000 + 5];
long long minn[4 * 300000 + 5];
long long sol;
void build(int v, int l, int r) {
  if (l > r) return;
  if (l == r) {
    minn[v] = res[l];
    lazy[v] = 0;
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  lazy[v] = 0;
  minn[v] = min(minn[2 * v], minn[2 * v + 1]);
}
void update(int v, int l, int r, int lt, int rt, int dx) {
  if (lazy[v] != 0) {
    minn[v] += lazy[v];
    if (l != r) {
      lazy[2 * v] += lazy[v];
      lazy[2 * v + 1] += lazy[v];
    }
    lazy[v] = 0;
  }
  if (l > r || lt > rt || l > rt || lt > r) return;
  if (lt <= l && r <= rt) {
    minn[v] += dx;
    if (l != r) {
      lazy[2 * v] += dx;
      lazy[2 * v + 1] += dx;
    }
    return;
  }
  int mid = (l + r) / 2;
  update(2 * v, l, mid, lt, rt, dx);
  update(2 * v + 1, mid + 1, r, lt, rt, dx);
  minn[v] = min(minn[2 * v], minn[2 * v + 1]);
}
long long query() {
  if (lazy[1] != 0) {
    minn[1] += lazy[1];
    lazy[2] += lazy[1];
    lazy[3] += lazy[1];
    lazy[1] = 0;
  }
  return minn[1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    poz[p[i]] = i;
  }
  for (int i = 1; i <= n; i++) cin >> a[p[i]];
  res[0] = a[1];
  for (int i = 1; i < n; i++) {
    res[i] = res[i - 1];
    if (p[i] == 1) {
      res[i] -= a[1];
    } else {
      res[i] += a[p[i]];
    }
  }
  build(1, 1, n - 1);
  sol = min((long long)a[p[1]], query());
  for (int i = 2; i <= n; i++) {
    update(1, 1, n - 1, 1, poz[i] - 1, +a[i]);
    update(1, 1, n - 1, poz[i], n - 1, -a[i]);
    sol = min(query(), sol);
  }
  cout << sol << "\n";
  return 0;
}
