#include <bits/stdc++.h>
using namespace std;
function<void(void)> ____ = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
const int MAXN = 2e5 + 7;
using LL = int_fast64_t;
int n, A[MAXN], cost[MAXN];
struct SegmentTree {
  LL minn[MAXN << 2], lazy[MAXN << 2];
  int l[MAXN << 2], r[MAXN << 2];
  void pushdown(int rt) {
    if (!lazy[rt]) return;
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    minn[rt << 1] += lazy[rt];
    minn[rt << 1 | 1] += lazy[rt];
    lazy[rt] = 0;
  }
  void build(int L, int R, int rt) {
    l[rt] = L, r[rt] = R;
    minn[rt] = lazy[rt] = 0;
    if (l[rt] + 1 == r[rt]) return;
    int mid = (L + R) >> 1;
    build(L, mid, rt << 1);
    build(mid, R, rt << 1 | 1);
  }
  void update(int L, int R, int rt, LL x) {
    if (l[rt] >= R || L >= r[rt]) return;
    if (L <= l[rt] && r[rt] <= R) {
      lazy[rt] += x;
      minn[rt] += x;
      return;
    }
    pushdown(rt);
    update(L, R, rt << 1, x);
    update(L, R, rt << 1 | 1, x);
    minn[rt] = min(minn[rt << 1], minn[rt << 1 | 1]);
  }
  LL qmin() { return minn[1]; }
} ST;
int main() {
  ____();
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> A[i];
  for (int i = 1; i <= n; i++) cin >> cost[i];
  LL res = 1e16;
  ST.build(0, n + 1, 1);
  ST.update(0, A[1], 1, cost[1]);
  for (int i = 2; i <= n; i++) ST.update(A[i], n + 1, 1, cost[i]);
  res = min(res, ST.qmin());
  for (int i = 2; i < n; i++) {
    ST.update(0, A[i], 1, cost[i]);
    ST.update(A[i], n + 1, 1, -cost[i]);
    res = min(res, ST.qmin());
  }
  cout << res << endl;
  return 0;
}
