#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long sum, pl;
} t[200086 << 2];
inline void up(int x) { t[x].sum = min(t[(x << 1)].sum, t[(x << 1 | 1)].sum); }
inline void add(int x, long long y) { t[x].sum += y, t[x].pl += y; }
inline void down(int x) {
  if (t[x].pl) {
    add((x << 1), t[x].pl), add((x << 1 | 1), t[x].pl);
    t[x].pl = 0;
  }
}
void modify(int x, int l, int r, int ll, int rr, int d) {
  if (ll <= l && r <= rr) {
    add(x, d);
    return;
  }
  down(x);
  int mid = l + r >> 1;
  if (mid >= ll) modify((x << 1), l, mid, ll, rr, d);
  if (mid < rr) modify((x << 1 | 1), mid + 1, r, ll, rr, d);
  up(x);
}
int n;
int p[200086], a[200086], b[200086];
int x;
long long ans = 1e18;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]), b[p[i]] = i;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n - 1; i++) modify(1, 1, n - 1, i, n - 1, a[i]);
  ans = min(ans, t[1].sum);
  for (int i = 1; i <= n; i++) {
    if (1 <= b[i] - 1) modify(1, 1, n - 1, 1, b[i] - 1, a[b[i]]);
    if (b[i] <= n - 1) modify(1, 1, n - 1, b[i], n - 1, -a[b[i]]);
    ans = min(ans, t[1].sum);
  }
  printf("%lld", ans);
}
