#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, ans, A[200005], P[200005],
    in[200005], lazy[800005], tree[800005];
void update(long long node, long long L, long long R, long long le,
            long long ri, long long val) {
  if (R < le || L > ri) return;
  if (le <= L && R <= ri) {
    lazy[node] += val;
    tree[node] += val;
    return;
  }
  lazy[2 * node] += lazy[node];
  lazy[2 * node + 1] += lazy[node];
  tree[2 * node] += lazy[node];
  tree[2 * node + 1] += lazy[node];
  lazy[node] = 0;
  long long mid = (L + R) / 2;
  update(2 * node, L, mid, le, ri, val);
  update(2 * node + 1, mid + 1, R, le, ri, val);
  tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
long long get(long long node, long long L, long long R, long long le,
              long long ri) {
  if (R < le || L > ri) return 99999999999999999;
  if (le <= L && R <= ri) return tree[node];
  lazy[2 * node] += lazy[node];
  lazy[2 * node + 1] += lazy[node];
  tree[2 * node] += lazy[node];
  tree[2 * node + 1] += lazy[node];
  lazy[node] = 0;
  long long mid = (L + R) / 2;
  return min(get(2 * node, L, mid, le, ri),
             get(2 * node + 1, mid + 1, R, le, ri));
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> P[i];
    in[P[i]] = i;
  }
  for (long long i = 1; i <= n; i++) {
    cin >> A[i];
    update(1, 1, n, i, n, A[i]);
  }
  ans = 99999999999999999;
  for (long long i = 1; i <= n; i++) {
    ans = min(ans, get(1, 1, n, 1, n - 1));
    update(1, 1, n, in[i], n, -A[in[i]]);
    update(1, 1, n, 1, in[i] - 1, A[in[i]]);
  }
  cout << min(ans, get(1, 1, n, 1, n - 1));
}
