#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long mod = 998244353;
int n;
int p[200010], a[200010];
long long val[200010], ans;
struct Tree {
  long long minv[200010 << 2], maxv[200010 << 2], addv[200010 << 2];
  inline void pushup(int o) {
    minv[o] = min(minv[(o << 1)], minv[(o << 1 | 1)]);
    maxv[o] = max(maxv[(o << 1)], maxv[(o << 1 | 1)]);
  }
  inline void pushdown(int o) {
    if (!addv[o]) return;
    addv[(o << 1)] += addv[o];
    addv[(o << 1 | 1)] += addv[o];
    maxv[(o << 1)] += addv[o];
    maxv[(o << 1 | 1)] += addv[o];
    minv[(o << 1)] += addv[o];
    minv[(o << 1 | 1)] += addv[o];
    addv[o] = 0;
  }
  inline void build(int o, int l, int r) {
    addv[o] = 0;
    if (l == r) {
      minv[o] = maxv[o] = val[l];
      return;
    }
    int mid = (l + r) >> 1;
    build((o << 1), l, mid);
    build((o << 1 | 1), mid + 1, r);
    pushup(o);
  }
  inline void update(int o, int l, int r, int ql, int qr, long long v) {
    if (ql <= l && r <= qr) {
      minv[o] += v;
      maxv[o] += v;
      addv[o] += v;
      return;
    }
    int mid = (l + r) >> 1;
    pushdown(o);
    if (ql <= mid) update((o << 1), l, mid, ql, qr, v);
    if (qr > mid) update((o << 1 | 1), mid + 1, r, ql, qr, v);
    pushup(o);
  }
  inline long long qmin(int o, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
      return minv[o];
    }
    int mid = (l + r) >> 1;
    long long ans = inf;
    pushdown(o);
    if (ql <= mid) ans = min(ans, qmin((o << 1), l, mid, ql, qr));
    if (qr > mid) ans = min(ans, qmin((o << 1 | 1), mid + 1, r, ql, qr));
    return ans;
  }
  inline long long qmax(int o, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
      return maxv[o];
    }
    int mid = (l + r) >> 1;
    long long ans = -inf;
    pushdown(o);
    if (ql <= mid) ans = max(ans, qmax((o << 1), l, mid, ql, qr));
    if (qr > mid) ans = max(ans, qmax((o << 1 | 1), mid + 1, r, ql, qr));
    return ans;
  }
} t;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", p + i);
  }
  t.build(1, 1, n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    t.update(1, 1, n + 1, p[i] + 1, n + 1, a[i]);
  }
  ans = inf;
  for (int i = 1; i < n; i++) {
    t.update(1, 1, n + 1, 1, p[i], a[i]);
    t.update(1, 1, n + 1, p[i] + 1, n + 1, -a[i]);
    ans = min(ans, t.qmin(1, 1, n + 1, 1, n + 1));
  }
  printf("%lld\n", ans);
  return 0;
}
