#include <bits/stdc++.h>
using namespace std;
long long tree[3 * 200200], lazy[3 * 200200];
long long w[200200];
long long prefix[200200];
long long p[200200], invp[200200];
long long ans;
int N;
void buildtree(int node, int l, int r) {
  if (l == r) {
    tree[node] = prefix[l];
  } else {
    buildtree(2 * node, l, (l + r) / 2);
    buildtree(2 * node + 1, (l + r) / 2 + 1, r);
    tree[node] = min(tree[2 * node], tree[2 * node + 1]);
  }
}
void propagate(int 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;
}
void update(int node, int l, int r, int ql, int qr, long long q) {
  if (l != r) {
    propagate(node);
  }
  if (l > qr || r < ql) {
    return;
  } else if (ql <= l && r <= qr) {
    tree[node] += q;
    lazy[node] += q;
    return;
  } else {
    update(2 * node, l, (l + r) / 2, ql, qr, q);
    update(2 * node + 1, (l + r) / 2 + 1, r, ql, qr, q);
    tree[node] = min(tree[2 * node], tree[2 * node + 1]);
  }
}
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> p[i];
    invp[p[i]] = i;
  }
  for (int i = 1; i <= N; i++) {
    cin >> w[i];
  }
  for (int i = 1; i <= N; i++) {
    prefix[i] = prefix[i - 1] + w[invp[i]];
  }
  ans = w[1];
  buildtree(1, 0, N);
  for (int i = 1; i < N; i++) {
    update(1, 0, N, 0, p[i] - 1, w[i]);
    update(1, 0, N, p[i], N, -w[i]);
    ans = min(ans, tree[1]);
  }
  cout << ans << endl;
}
