#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const double PI = acos(-1.0);
const long long MOD = 1000000007LL;
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;
}
template <typename T>
struct LazySegmentTree {
  const T inf = numeric_limits<T>::max();
  int N;
  vector<T> node, lazy;
  LazySegmentTree() = default;
  LazySegmentTree(vector<T> &v) { init(v); }
  void init(vector<T> &v) {
    int sz = v.size();
    N = 1;
    while (N < sz) N *= 2;
    node.assign(2 * N, inf);
    lazy.assign(2 * N, 0);
    for (int i = 0; i < sz; i++) node[i + N] = v[i];
    for (int i = N - 1; i > 0; i--)
      node[i] = min(node[i << 1 | 0], node[i << 1 | 1]);
  }
  void eval(int k) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (k < N) {
        lazy[k << 1 | 0] += lazy[k];
        lazy[k << 1 | 1] += lazy[k];
      }
      lazy[k] = 0;
    }
  }
  void update(int a, int b, T val, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = N;
    eval(k);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] += val;
      eval(k);
    } else {
      update(a, b, val, k << 1 | 0, l, (l + r) / 2);
      update(a, b, val, k << 1 | 1, (l + r) / 2, r);
      node[k] = min(node[k << 1 | 0], node[k << 1 | 1]);
    }
  }
  T get(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = N;
    eval(k);
    if (b <= l || r <= a) return inf;
    if (a <= l && r <= b) return node[k];
    T vl = get(a, b, k << 1 | 0, l, (l + r) / 2);
    T vr = get(a, b, k << 1 | 1, (l + r) / 2, r);
    return min(vl, vr);
  }
  T operator[](int x) {
    x += N;
    eval(x);
    return node[x];
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<long long> p(N), A(N);
  for (int i = 0; i < (int)(N); ++i) cin >> p[i];
  for (int i = 0; i < (int)(N); ++i) cin >> A[i];
  vector<long long> cost(N + 1);
  for (int i = 0; i < (int)(N); ++i) cost[p[i]] = A[i];
  for (int i = 0; i < (int)(N); ++i) cost[i + 1] += cost[i];
  LazySegmentTree<long long> seg(cost);
  long long ans = 1e18;
  for (int i = 0; i < (int)(N - 1); ++i) {
    seg.update(p[i], N + 1, -A[i]);
    seg.update(0, p[i], A[i]);
    chmin(ans, seg.get(0, N + 1));
  }
  cout << ans << '\n';
}
