#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const long long mas = 262144 * 4 + 10;
long long tree[mas], mn[mas], mx[mas];
void build() {
  long long z = 262144;
  long long mun = 1, mux = z;
  long long kf = 1;
  while (z) {
    tree[kf] = 0;
    mn[kf] = mun;
    mx[kf] = mux;
    kf++;
    mun = mux + 1;
    mux = mun + z - 1;
    if (mun > 262144) {
      z /= 2;
      mun = 1;
      mux = z;
    }
  }
}
long long boom;
void plu(long long num, long long elem, long long mun, long long mux) {
  mun = max(mun, mn[num]);
  mux = min(mux, mx[num]);
  if (mun > mux) return;
  if (mun == mn[num] && mux == mx[num]) {
    tree[num] += elem;
    return;
  }
  plu(num * 2, elem, mun, mux);
  plu(num * 2 + 1, elem, mun, mux);
  boom = min(tree[num * 2], tree[num * 2 + 1]);
  tree[num] += boom;
  tree[num * 2] -= boom;
  tree[num * 2 + 1] -= boom;
  return;
}
long long min_tree(long long num, long long mun, long long mux) {
  mun = max(mun, mn[num]);
  mux = min(mux, mx[num]);
  if (mun > mux) return 100000000000;
  if (mun == mn[num] && mux == mx[num]) {
    return tree[num];
  }
  return max(tree[num],
             min(min_tree(num * 2, mun, mux), min_tree(num * 2 + 1, mun, mux)));
}
int main() {
  fast();
  build();
  long long n;
  cin >> n;
  long long p[n + 10], a[n + 10];
  for (int i = 1; i <= n; ++i) cin >> p[i];
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    plu(1, a[i], i, n);
  }
  long long ans = 10000000000000;
  long long arcp[n + 10];
  for (int i = 1; i <= n; ++i) arcp[p[i]] = i;
  for (int i = 1; i <= n + 1; ++i) {
    ans = min(ans, min_tree(1, 1, n - 1));
    if (i <= n) {
      plu(1, a[arcp[i]], 1, arcp[i] - 1);
      plu(1, -a[arcp[i]], arcp[i], n);
    }
  }
  cout << ans;
  return 0;
}
