#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
long long ans[M << 2], tag[M << 2];
int a[M], p[M];
void push_up(int p) { ans[p] = min(ans[p << 1], ans[p << 1 | 1]); }
void ps(int p, int l, int r, long long x) {
  ans[p] += x;
  tag[p] += x;
}
void push_down(int p, int l, int r) {
  if (!tag[p]) return;
  int mid = l + r >> 1;
  ps(p << 1, l, mid, tag[p]);
  ps(p << 1 | 1, mid + 1, r, tag[p]);
  tag[p] = 0;
}
void add(int p, int p_l, int p_r, int l, int r, int x) {
  if (r < l) return;
  if (p_l >= l && p_r <= r) {
    ans[p] += x;
    tag[p] += x;
    return;
  }
  push_down(p, p_l, p_r);
  int mid = p_l + p_r >> 1;
  if (l <= mid) add(p << 1, p_l, mid, l, r, x);
  if (r > mid) add(p << 1 | 1, mid + 1, p_r, l, r, x);
  push_up(p);
}
int main() {
  int n;
  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]);
    add(1, 1, n, p[i] + 1, n, a[i]);
  }
  long long sum = min(a[1], a[n]);
  for (int i = 1; i < n; i++) {
    add(1, 1, n, p[i] + 1, n, -a[i]);
    add(1, 1, n, 1, p[i] - 1, a[i]);
    sum = min(ans[1], sum);
  }
  printf("%lld\n", sum);
  return 0;
}
