#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, mod = 1e9 + 7, INF = 0x3f3f3f3f;
const double eps = 1e-6;
int n;
int p[N], a[N];
struct Tree {
  struct Node {
    int l, r;
    long long mi, lazy;
  } tr[N << 2];
  void pushup(int u) { tr[u].mi = min(tr[(u << 1)].mi, tr[(u << 1 | 1)].mi); }
  void pushdown(int u) {
    if (tr[u].lazy == 0) return;
    long long lazy = tr[u].lazy;
    tr[u].lazy = 0;
    tr[(u << 1)].lazy += lazy;
    tr[(u << 1)].mi += lazy;
    tr[(u << 1 | 1)].lazy += lazy;
    tr[(u << 1 | 1)].mi += lazy;
  }
  void build(int u, int l, int r) {
    tr[u] = {l, r, 0, 0};
    if (l == r) {
      tr[u].mi = 0;
      return;
    }
    build((u << 1), l, (tr[u].l + tr[u].r >> 1));
    build((u << 1 | 1), (tr[u].l + tr[u].r >> 1) + 1, r);
  }
  void modify(int u, int l, int r, int x) {
    if (tr[u].l >= l && tr[u].r <= r) {
      tr[u].mi += x;
      tr[u].lazy += x;
      return;
    }
    pushdown(u);
    if (l <= (tr[u].l + tr[u].r >> 1)) modify((u << 1), l, r, x);
    if (r > (tr[u].l + tr[u].r >> 1)) modify((u << 1 | 1), l, r, x);
    pushup(u);
  }
} x;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  long long ans = min(a[1], a[n]);
  x.build(1, 1, n);
  for (int i = 1; i <= n; i++) x.modify(1, p[i], n, a[i]);
  for (int i = 1; i <= n - 1; i++) {
    x.modify(1, 0, p[i] - 1, a[i]);
    x.modify(1, p[i], n, -a[i]);
    ans = min(ans, x.tr[1].mi);
  }
  printf("%lld\n", ans);
  return 0;
}
