#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int INF = INT_MAX / 2;
const long long LLINF = 9223372036854775807 / 2;
long long Add[200007 << 2];
long long A[200007], n;
long long Min[200007 << 2];
void PushUp(int rt) { Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]); }
void Build(int l, int r, int rt) {
  if (l == r) {
    Min[rt] = A[l];
    return;
  }
  int m = (l + r) >> 1;
  Build(l, m, rt << 1);
  Build(m + 1, r, rt << 1 | 1);
  PushUp(rt);
}
void PushDown(int rt, int ln, int rn) {
  if (Add[rt]) {
    Add[rt << 1] += Add[rt];
    Add[rt << 1 | 1] += Add[rt];
    Min[rt << 1] += Add[rt];
    Min[rt << 1 | 1] += Add[rt];
    Add[rt] = 0;
  }
}
void Update(int L, int R, int C, int l, int r, int rt) {
  if (L <= l && r <= R) {
    Add[rt] += C;
    Min[rt] += C;
    return;
  }
  int m = (l + r) >> 1;
  PushDown(rt, m - l + 1, r - m);
  if (L <= m) Update(L, R, C, l, m, rt << 1);
  if (R > m) Update(L, R, C, m + 1, r, rt << 1 | 1);
  PushUp(rt);
}
long long Query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) {
    return Min[rt];
  }
  int m = (l + r) >> 1;
  PushDown(rt, m - l + 1, r - m);
  long long ANS = LLINF;
  if (L <= m) ANS = min(ANS, Query(L, R, l, m, rt << 1));
  if (R > m) ANS = min(ANS, Query(L, R, m + 1, r, rt << 1 | 1));
  return ANS;
}
int p[200007], a[200007];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int ans;
    scanf("%d", &ans);
    p[ans] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    A[i] = A[i - 1] + a[i];
  }
  memset(Min, 0x3f3f3f3f, sizeof(Min));
  Build(1, n, 1);
  long long ANS = Query(1, n - 1, 1, n, 1);
  for (int y = 1; y <= n; y++) {
    int pi = p[y];
    if (pi - 1 >= 1) Update(1, pi - 1, a[pi], 1, n, 1);
    Update(pi, n, -a[pi], 1, n, 1);
    ANS = min(ANS, Query(1, n - 1, 1, n, 1));
  }
  printf("%lld\n", ANS);
}
