#include <bits/stdc++.h>
const int N = 200005;
const long long inf = (long long)1e18;
int n, p[N], a[N];
struct tree {
  long long mn, tag;
} t[N * 4];
void mark(int d, long long w) {
  t[d].mn += w;
  t[d].tag += w;
}
void pushdown(int d) {
  if (!t[d].tag) return;
  long long w = t[d].tag;
  t[d].tag = 0;
  mark(d << 1, w);
  mark(d << 1 | 1, w);
}
void ins(int d, int l, int r, int x, int y, long long z) {
  if (x <= l && r <= y) {
    mark(d, z);
    return;
  }
  pushdown(d);
  int mid = (l + r) / 2;
  if (x <= mid) ins(d << 1, l, mid, x, y, z);
  if (y > mid) ins(d << 1 | 1, mid + 1, r, x, y, z);
  t[d].mn = std::min(t[d << 1].mn, t[d << 1 | 1].mn);
}
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]);
  for (int i = 1; i <= n; i++) ins(1, 0, n, p[i], n, a[i]);
  long long ans = inf;
  for (int i = 1; i < n; i++) {
    ins(1, 0, n, p[i], n, -a[i]);
    ins(1, 0, n, 0, p[i] - 1, a[i]);
    ans = std::min(ans, t[1].mn);
  }
  printf("%lld\n", ans);
  return 0;
}
