#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
class ST {
  int n;
  long long *a, *t, *o;
  inline void build(int v, int l, int r) {
    o[v] = 0;
    if (l == r) {
      t[v] = a[l];
    } else {
      int m = (r + l) >> 1;
      build(v + v + 1, l, m);
      build(v + v + 2, m + 1, r);
      t[v] = min(t[v + v + 1], t[v + v + 2]);
    }
  }
  inline void update(int v, int l, int r, int pos, int val) {
    if (l == r) {
      t[v] += val;
    } else {
      int m = (r + l) >> 1;
      if (pos <= m) {
        update(v + v + 1, l, m, pos, val);
      } else {
        update(v + v + 2, m + 1, r, pos, val);
      }
      t[v] = min(t[v + v + 1], t[v + v + 2]);
    }
  }
  inline void push(int v) {
    t[v] += o[v];
    if (v + v + 2 < 4 * n) {
      o[v + v + 1] += o[v];
      o[v + v + 2] += o[v];
    }
    o[v] = 0;
  }
  inline void update(int v, int l, int r, int tl, int tr, int val) {
    if (tl > tr) return;
    if (tl == l && tr == r) {
      o[v] += val;
      push(v);
    } else {
      push(v);
      int m = (r + l) >> 1;
      update(v + v + 1, l, m, tl, min(m, tr), val);
      update(v + v + 2, m + 1, r, max(m + 1, tl), tr, val);
      t[v] = min(t[v + v + 1], t[v + v + 2]);
    }
  }
  inline long long query(int v, int l, int r, int tl, int tr) {
    push(v);
    if (tl > tr) return 1e18;
    if (l == tl && r == tr) {
      return t[v];
    }
    int m = (r + l) >> 1;
    long long t1 = query(v + v + 1, l, m, tl, min(tr, m));
    long long t2 = query(v + v + 2, m + 1, r, max(tl, m + 1), tr);
    return min(t1, t2);
  }

 public:
  ST() {}
  ST(int n) : n(n) {
    t = new long long[4 * n];
    o = new long long[4 * n];
    a = new long long[n];
    for (int i = 0; i < n; ++i) a[i] = 0;
    build(0, 0, n - 1);
  }
  ST(int n, long long *a) : n(n), a(a) {
    t = new long long[4 * n];
    o = new long long[4 * n];
    build(0, 0, n - 1);
  }
  void update(int pos, int val) { update(0, 0, n - 1, pos, val); }
  void update(int l, int r, int val) { update(0, 0, n - 1, l, r, val); }
  long long query(int l, int r) { return query(0, 0, n - 1, l, r); }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  int p[n], pos[n + 1];
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    pos[p[i]] = i;
  }
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long b[n + 1], sum = 0;
  for (int i = 0; i < n; ++i) {
    b[i] = sum, sum += a[i];
  }
  b[n] = sum;
  ST t = ST(n + 1, b);
  long long ans = t.query(1, n - 1);
  for (int val = 1; val <= n; ++val) {
    t.update(0, pos[val], a[pos[val]]);
    t.update(pos[val] + 1, n, -a[pos[val]]);
    ans = min(ans, t.query(1, n - 1));
  }
  cout << ans;
}
