#include <bits/stdc++.h>
using namespace std;
constexpr int kN = int(2E5 + 10);
struct seg_tree {
  long long int val[kN << 2], flag[kN << 2];
  void init() {
    memset(flag, 0, sizeof(flag));
    return;
  }
  void addtag(int n, long long int x) {
    val[n] += x;
    flag[n] += x;
    return;
  }
  void pull(int n) {
    val[n] = min(val[n * 2 + 1], val[n * 2 + 2]);
    return;
  }
  void push(int n) {
    if (flag[n]) {
      addtag(n * 2 + 1, flag[n]);
      addtag(n * 2 + 2, flag[n]);
      flag[n] = 0;
    }
    return;
  }
  void fix(int n, int l, int r, int L, int R, long long int x) {
    if (L <= l && r <= R)
      addtag(n, x);
    else if (!(l > R || L > r)) {
      int mid = (l + r) >> 1;
      push(n);
      fix(n * 2 + 1, l, mid, L, R, x);
      fix(n * 2 + 2, mid + 1, r, L, R, x);
      pull(n);
    }
    return;
  }
};
int p[kN], a[kN];
seg_tree sg;
int main() {
  int n;
  long long int ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) ans += a[i];
  sg.init();
  for (int i = 1; i <= n; i++) sg.fix(0, 0, n, p[i], n, a[i]);
  for (int i = 1; i < n; i++) {
    sg.fix(0, 0, n, p[i], n, -a[i]);
    sg.fix(0, 0, n, 0, p[i] - 1, a[i]);
    ans = min(ans, sg.val[0]);
  }
  printf("%lld\n", ans);
}
