#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct SegTree {
  vector<long long> tree, lazy;
  int base;
  SegTree(int a) {
    base = 1;
    while (base < a) base <<= 1;
    tree.resize(base * 2 + 2, INF);
    lazy.resize(base * 2 + 2);
    base--;
  }
  void propagate(int ns, int nf, int num) {
    if (lazy[num] != 0) {
      if (ns < nf) {
        lazy[num * 2] += lazy[num];
        lazy[num * 2 + 1] += lazy[num];
      }
      tree[num] += lazy[num];
      lazy[num] = 0;
    }
  }
  void update(int idx, long long val) {
    idx += base;
    tree[idx] = val;
    idx >>= 1;
    while (idx != 0) {
      tree[idx] = min(tree[idx * 2], tree[idx * 2 + 1]);
      idx >>= 1;
    }
  }
  long long query(int st, int fn, int ns = 1, int nf = -1, int num = 1) {
    if (nf == -1) nf = base + 1;
    propagate(ns, nf, num);
    if (ns > fn || nf < st) return INF;
    if (st <= ns && nf <= fn) return tree[num];
    int mid = (ns + nf) >> 1;
    return min(query(st, fn, ns, mid, num * 2),
               query(st, fn, mid + 1, nf, num * 2 + 1));
  }
  void add_value(long long val, int st, int fn, int ns = 1, int nf = -1,
                 int num = 1) {
    if (nf == -1) nf = base + 1;
    propagate(ns, nf, num);
    if (ns > fn || nf < st) return;
    if (st <= ns && nf <= fn) {
      lazy[num] += val;
      propagate(ns, nf, num);
      return;
    }
    int mid = (ns + nf) >> 1;
    add_value(val, st, fn, ns, mid, num * 2);
    add_value(val, st, fn, mid + 1, nf, num * 2 + 1);
    tree[num] = min(tree[num * 2], tree[num * 2 + 1]);
  }
};
int n, m[200005];
long long a[200005], b[200005], s[200005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &m[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    b[m[i]] = a[i];
  }
  SegTree T(n);
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + b[i];
    T.update(i, s[i]);
  }
  if (m[1] > 1) T.add_value(a[1], 1, m[1] - 1);
  T.add_value(-a[1], m[1], n);
  long long ans = T.query(1, n);
  for (int i = 2; i < n; i++) {
    if (m[i] > 1) T.add_value(a[i], 1, m[i] - 1);
    T.add_value(-a[i], m[i], n);
    ans = min(ans, T.query(1, n));
  }
  ans = min(ans, a[1]);
  printf("%lld", ans);
  return 0;
}
