#include <bits/stdc++.h>
using namespace std;
template <typename TYPE>
inline void chkmax(TYPE &first, TYPE second) {
  first < second ? first = second : 0;
}
template <typename TYPE>
inline void chkmin(TYPE &first, TYPE second) {
  second < first ? first = second : 0;
}
template <typename TYPE>
void readint(TYPE &first) {
  first = 0;
  int f = 1;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) first = first * 10 + c - '0';
  first *= f;
}
const int MAXN = 200005;
int n, p[MAXN], a[MAXN], b[MAXN];
long long sum[MAXN << 2], pre[MAXN << 2];
void build(int id, int l, int r) {
  if (l == r) {
    sum[id] = -b[l];
    pre[id] = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  sum[id] = sum[id << 1] + sum[id << 1 | 1];
  pre[id] = max(pre[id << 1], sum[id << 1] + pre[id << 1 | 1]);
}
void modify(int id, int l, int r, int first, int c) {
  if (l == r) {
    sum[id] = pre[id] = c;
    return;
  }
  int mid = (l + r) >> 1;
  if (first <= mid)
    modify(id << 1, l, mid, first, c);
  else
    modify(id << 1 | 1, mid + 1, r, first, c);
  sum[id] = sum[id << 1] + sum[id << 1 | 1];
  pre[id] = max(pre[id << 1], sum[id << 1] + pre[id << 1 | 1]);
}
int main() {
  readint(n);
  for (int i = 1; i <= n; ++i) readint(p[i]);
  for (int i = 1; i <= n; ++i) readint(a[i]), b[p[i]] = a[i];
  build(1, 1, n);
  long long sum = 0, ans = 1ll << 60;
  for (int i = 1; i < n; ++i) {
    modify(1, 1, n, p[i], a[i]);
    sum += a[i];
    chkmin(ans, sum - pre[1]);
  }
  printf("%lld\n", ans);
  return 0;
}
