#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 1e18 + 7;
int n, p[N], a[N];
struct SegmentTree {
  long long d[N * 4], lazy[N * 4];
  void down(int id) {
    if (lazy[id] != 0) {
      d[id << 1] += lazy[id];
      d[id << 1 | 1] += lazy[id];
      lazy[id << 1] += lazy[id];
      lazy[id << 1 | 1] += lazy[id];
      lazy[id] = 0;
    }
  }
  void update(int id, int l, int r, int u, int v, long long val) {
    if (v < l || r < u || l > r) return;
    if (u <= l && r <= v) {
      d[id] += val;
      lazy[id] += val;
      return;
    }
    down(id);
    int mid = (l + r) >> 1;
    update(id << 1, l, mid, u, v, val);
    update(id << 1 | 1, mid + 1, r, u, v, val);
    d[id] = min(d[id << 1], d[id << 1 | 1]);
  }
  long long query(int id, int l, int r, int u, int v) {
    if (v < l || r < u || l > r) return INF;
    if (u <= l && r <= v) return d[id];
    down(id);
    int mid = (l + r) >> 1;
    return min(query(id << 1, l, mid, u, v),
               query(id << 1 | 1, mid + 1, r, u, v));
  }
} ST;
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]);
    ST.update(1, 1, n, p[i], n, a[i]);
  }
  long long ans = INF, tmp = 0;
  for (int i = 1; i <= n - 1; i++) {
    tmp += a[i];
    ans = min(ans, tmp);
  }
  for (int i = 1; i <= n - 1; i++) {
    ST.update(1, 1, n, p[i], n, -a[i]);
    ST.update(1, 1, n, 1, p[i] - 1, +a[i]);
    ans = min(ans, ST.query(1, 1, n, 1, n));
  }
  printf("%I64d", ans);
}
