#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)(1e15);
struct Node {
  Node *l = 0, *r = 0;
  long long lo, hi, mset = inf, madd = 0, val = -inf;
  Node(long long lo, long long hi) : lo(lo), hi(hi) {}
  Node(vector<long long> &v, long long lo, long long hi) : lo(lo), hi(hi) {
    if (lo + 1 < hi) {
      long long mid = lo + (hi - lo) / 2LL;
      l = new Node(v, lo, mid);
      r = new Node(v, mid, hi);
      val = max(l->val, r->val);
    } else
      val = v[lo];
  }
  long long query(long long L, long long R) {
    if (R <= lo || hi <= L) return -inf;
    if (L <= lo && hi <= R) return val;
    push();
    return max(l->query(L, R), r->query(L, R));
  }
  void set(long long L, long long R, long long x) {
    if (R <= lo || hi <= L) return;
    if (L <= lo && hi <= R)
      mset = val = x, madd = 0;
    else {
      push(), l->set(L, R, x), r->set(L, R, x);
      val = max(l->val, r->val);
    }
  }
  void add(long long L, long long R, long long x) {
    if (R <= lo || hi <= L) return;
    if (L <= lo && hi <= R) {
      if (mset != inf)
        mset = madd + x;
      else
        madd = madd + x;
      val += x;
    } else {
      push(), l->add(L, R, x), r->add(L, R, x);
      val = max(l->val, r->val);
    }
  }
  void push() {
    if (!l) {
      long long mid = lo + (hi - lo) / 2;
      l = new Node(lo, mid);
      r = new Node(mid, hi);
    }
    if (mset != inf)
      l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
    else if (madd)
      l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
  }
};
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  long long n;
  cin >> n;
  vector<long long> p(n, 0), a(n, 0), loc(n + 1, 0);
  for (int i = 0; i < (n); ++i) {
    cin >> p[i];
    loc[p[i]] = i;
  }
  for (int i = 0; i < (n); ++i) cin >> a[i];
  vector<long long> v(4e5, 0);
  Node *tr = new Node(v, 0, n + 1);
  for (int i = 1; i < (n + 1); ++i) {
    tr->add(i, n + 1, -a[loc[i]]);
  }
  long long best = inf;
  for (int i = 1; i < (n); ++i) {
    long long x = p[i - 1];
    tr->add(x, n + 1, a[i - 1]);
    tr->add(0, x, -a[i - 1]);
    best = min(best, -tr->query(0, n + 1));
  }
  cout << best << endl;
}
