#include <bits/stdc++.h>
const int MAXN = 2e5;
const int TSIZE = 1 << 19;
const int TSTART = TSIZE / 2;
long long T[TSIZE];
long long P[TSIZE];
int p[MAXN];
int pinv[MAXN + 1];
int a[MAXN];
long long max(long long a, long long b) { return a < b ? b : a; }
int max(int a, int b) { return a < b ? b : a; }
int min(int a, int b) { return a < b ? a : b; }
void update(int x, long long v, int l, int r, int LL, int RR) {
  int m = min(r, RR);
  int M = max(l, LL);
  if (M <= m) {
    if (M == LL && m == RR) {
      P[x] += v;
      T[x] += v;
      return;
    }
    long long pp = P[x];
    if (pp != 0) {
      P[x] = 0;
      T[x * 2] += pp;
      T[x * 2 + 1] += pp;
      P[x * 2] += pp;
      P[x * 2 + 1] += pp;
    }
    int mid = (LL + RR) / 2;
    update(x * 2, v, l, r, LL, mid);
    update(x * 2 + 1, v, l, r, mid + 1, RR);
    T[x] = max(T[x * 2], T[x * 2 + 1]);
  }
}
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &p[i]);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i) pinv[p[i]] = i + 1;
  for (int i = 0; i < TSIZE; ++i) T[i] = -8e18;
  T[TSTART] = -a[0];
  for (int i = 1; i < n - 1; ++i) T[i + TSTART] = T[i - 1 + TSTART] - a[i];
  for (int i = TSTART - 1; i > 0; --i) T[i] = max(T[i * 2], T[i * 2 + 1]);
  long long res = T[1];
  for (int i = 1; i <= n; ++i) {
    int pi = pinv[i];
    int ai = a[pi - 1];
    if (pi > 1) update(1, -ai, 1, pi - 1, 1, TSTART);
    if (pi < n) update(1, ai, pi, n - 1, 1, TSTART);
    res = max(res, T[1]);
  }
  printf("%I64d\n", -res);
}
int main() {
  solve();
  return 0;
}
