#include <bits/stdc++.h>
using namespace std;
long long n, f[1000010], lz[1000010], a[500010], b[500010], ans;
void pushdown(long long x) {
  if (lz[x]) {
    f[x * 2] += lz[x];
    f[x * 2 + 1] += lz[x];
    lz[x * 2] += lz[x];
    lz[x * 2 + 1] += lz[x];
    lz[x] = 0;
  }
}
void ins(long long x, long long l, long long r, long long t, long long k,
         long long w) {
  if (l == t && r == k) {
    f[x] += w;
    lz[x] += w;
    return;
  }
  pushdown(x);
  long long mid = (l + r) / 2;
  if (mid >= k)
    ins(x * 2, l, mid, t, k, w);
  else if (t > mid)
    ins(x * 2 + 1, mid + 1, r, t, k, w);
  else
    ins(x * 2, l, mid, t, mid, w), ins(x * 2 + 1, mid + 1, r, mid + 1, k, w);
  f[x] = min(f[x * 2], f[x * 2 + 1]);
}
int main() {
  long long i;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (i = 1; i <= n; i++) scanf("%lld", &b[i]);
  for (i = 1; i <= n; i++) ins(1, 0, n, a[i], n, b[i]);
  ans = 1e18;
  for (i = 1; i < n; i++) {
    ins(1, 0, n, a[i], n, -b[i]);
    ins(1, 0, n, 0, a[i] - 1, b[i]);
    ans = min(ans, f[1]);
  }
  printf("%lld", ans);
}
