#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, inf = 0x3f3f3f3f;
int n, a[N], w[N];
struct D {
  long long l, r, ans;
} tr[N << 2];
D operator+(const D& a, const D& b) {
  D c;
  c.l = a.l + b.l;
  c.r = a.r + b.r;
  c.ans = min(a.r + b.ans, a.ans + b.l);
  return c;
}
void pu(int u) { tr[u] = tr[(u << 1)] + tr[(u << 1 | 1)]; }
void build(int u = 1, int l = 1, int r = n) {
  if (l == r) {
    tr[u] = {0, w[l], 0};
    return;
  }
  build((u << 1), l, ((l + r) >> 1)),
      build((u << 1 | 1), ((l + r) >> 1) + 1, r), pu(u);
}
void upd(int p, int u = 1, int l = 1, int r = n) {
  if (l == r) {
    tr[u] = {w[l], 0, 0};
    return;
  }
  p <= ((l + r) >> 1) ? upd(p, (u << 1), l, ((l + r) >> 1))
                      : upd(p, (u << 1 | 1), ((l + r) >> 1) + 1, r);
  pu(u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &w[a[i]]);
  build();
  long long ans = 0x3f3f3f3f3f3f3f3fll;
  for (int i = 1; i < n; ++i) upd(a[i]), ans = min(ans, tr[1].ans);
  printf("%lld\n", ans);
  return 0;
}
