#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V<V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
template <typename T>
struct LazySegmentTree {
  int n;
  vector<T> data, lazy;
  LazySegmentTree(vector<T> vec) {
    int sz = vec.size();
    n = 1;
    while (n < sz) n *= 2;
    data.resize(2 * n - 1, INF);
    lazy.resize(2 * n - 1, 0);
    for (int i = 0; i < sz; i++) data[i + n - 1] = vec[i];
    for (int i = n - 2; i >= 0; i--)
      data[i] = min(data[2 * i + 1], data[2 * i + 2]);
  }
  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      data[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k];
        lazy[2 * k + 2] += lazy[k];
      }
      lazy[k] = 0;
    }
  }
  void add(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] += x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) >> 1);
      add(a, b, x, 2 * k + 2, (l + r) >> 1, r);
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  T get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (b <= l || r <= a) return 0;
    eval(k, l, r);
    if (a <= l && r <= b) return data[k];
    T vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vii p(n), a(n), cosa(n);
  for (ll i = 0; i < n; i++) cin >> p[i];
  for (ll i = 0; i < n; i++) cin >> a[i];
  for (ll i = 0; i < n; i++) cosa[p[i] - 1] = a[i];
  vll acca(n + 1, 0);
  for (ll i = 0; i < n; i++) acca[i + 1] = acca[i] + cosa[i];
  LazySegmentTree<ll> lseg(acca);
  ll ans = HIGHINF, tmpa = 0;
  for (ll i = 0; i < n - 1; i++) {
    tmpa += a[i];
    lseg.add(p[i], n + 2, ll(-a[i]) * 2);
    chmin(ans, tmpa + lseg.get(1, n + 1));
  }
  cout << ans << '\n';
  return 0;
}
