#include <bits/stdc++.h>
using namespace std;
const int MAXn = 2e5 + 3;
const long long INF = 1e15 + 1;
long long t[4 * MAXn], sum[MAXn], lazy[4 * MAXn];
int id[MAXn];
void Build(int v, int l, int r) {
  lazy[v] = 0;
  if (l == r) {
    t[v] = sum[l];
    return;
  }
  int mid = (r + l) / 2;
  Build(v * 2, l, mid);
  Build(v * 2 + 1, mid + 1, r);
  t[v] = min(t[v * 2], t[v * 2 + 1]);
  return;
}
void Down(int v) {
  t[v * 2] += lazy[v];
  lazy[v * 2] += lazy[v];
  t[v * 2 + 1] += lazy[v];
  lazy[v * 2 + 1] += lazy[v];
  lazy[v] = 0;
}
void Update(int v, int l, int r, int L, int R, long long x) {
  if (r < L || R < l) return;
  if (L <= l && r <= R) {
    lazy[v] += x;
    t[v] += x;
    return;
  }
  int mid = (r + l) / 2;
  Down(v);
  Update(v * 2, l, mid, L, R, x);
  Update(v * 2 + 1, mid + 1, r, L, R, x);
  t[v] = min(t[v * 2], t[v * 2 + 1]);
  return;
}
long long get(int v, int l, int r, int L, int R) {
  if (r < L || R < l) return INF;
  if (L <= l && R >= r) return t[v];
  int mid = (l + r) / 2;
  Down(v);
  return min(get(v * 2, l, mid, L, R), get(v * 2 + 1, mid + 1, r, L, R));
}
int main() {
  int n;
  cin >> n;
  int val;
  for (int i = 1; i <= n; i++) {
    cin >> val;
    id[val] = i;
  }
  sum[0] = 0;
  long long p;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    sum[i] = sum[i - 1] + p;
  }
  Build(1, 1, n - 1);
  long long ans = get(1, 1, n - 1, 1, n - 1);
  for (int val = 1; val <= n; val++) {
    if (id[val] > 1)
      Update(1, 1, n - 1, 1, id[val] - 1, sum[id[val]] - sum[id[val] - 1]);
    if (id[val] < n) {
      Update(1, 1, n - 1, id[val], n - 1, -sum[id[val]] + sum[id[val] - 1]);
      ans = min(ans, get(1, 1, n - 1, id[val], n - 1));
    }
  }
  cout << ans << endl;
  return 0;
}
