#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 1e18;
const int MAXN = 2e5 + 10;
const int mod = 1e9 + 7;
int p[MAXN], pos[MAXN];
long long a[MAXN], per[MAXN];
long long lazy[MAXN << 2], maxx[MAXN << 2];
inline void pushup(int rt) { maxx[rt] = min(maxx[rt << 1], maxx[rt << 1 | 1]); }
inline void pushdown(int rt) {
  if (lazy[rt] != 0) {
    maxx[rt << 1] += lazy[rt];
    maxx[rt << 1 | 1] += lazy[rt];
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    lazy[rt] = 0;
  }
}
void build(int l, int r, int rt) {
  int m = l + r >> 1;
  if (l == r) {
    maxx[rt] = per[l];
    lazy[rt] = 0;
    return;
  }
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  pushup(rt);
}
void update(long long val, int L, int R, int l, int r, int rt) {
  if (l >= L && r <= R) {
    maxx[rt] -= val;
    lazy[rt] -= val;
    return;
  }
  pushdown(rt);
  int m = l + r >> 1;
  if (L <= m) update(val, L, R, l, m, rt << 1);
  if (R > m) update(val, L, R, m + 1, r, rt << 1 | 1);
  pushup(rt);
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long ans = 1e15;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
    pos[p[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    per[i] = per[i - 1] + a[pos[i]];
  }
  build(1, n, 1);
  long long cur = 0;
  for (int i = 1; i < n; ++i) {
    cur += a[i];
    ans = min(ans, min(cur, per[n] - cur));
    update(a[i] * 2, p[i], n, 1, n, 1);
    ans = min(ans, cur + maxx[1]);
  }
  cout << ans << endl;
  return 0;
}
