#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
const long long N = 3e5 + 5;
const long long mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
long long test = 1, n, a[N], ind[N], pre[N], b[N];
long long tree[4 * N + 8], lazy[4 * N + 8];
void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = pre[start];
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = min(tree[2 * node], tree[2 * node + 1]);
  }
}
void pushlazy(int start, int end, int node, long long val) {
  tree[node] += val;
  if (start != end) {
    lazy[node * 2] += val;
    lazy[node * 2 + 1] += val;
  }
  lazy[node] = 0;
}
void update(int node, int start, int end, int l, int r, long long val) {
  pushlazy(start, end, node, lazy[node]);
  if (start > end or start > r or end < l) return;
  if (start >= l and end <= r) {
    pushlazy(start, end, node, val);
    return;
  }
  int mid = (start + end) / 2;
  update(node * 2, start, mid, l, r, val);
  update(node * 2 + 1, mid + 1, end, l, r, val);
  tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}
long long query(int node, int start, int end, int l, int r) {
  if (start > end or start > r or end < l) return +INF;
  pushlazy(start, end, node, lazy[node]);
  if (l <= start and end <= r) return tree[node];
  int mid = (start + end) / 2;
  long long p1 = query(2 * node, start, mid, l, r);
  long long p2 = query(2 * node + 1, mid + 1, end, l, r);
  return min(p1, p2);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> b[i], ind[b[i]] = i;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[ind[i]];
  long long ans = a[1];
  build(1, 1, n);
  for (int i = 1; i < n; i++) {
    update(1, 1, n, b[i], n, -a[i]);
    update(1, 1, n, 1, b[i] - 1, +a[i]);
    ans = min(ans, query(1, 1, n, 1, n));
  }
  cout << ans << " "
       << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  ;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}
