#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int MX = 2e5 + 5;
template <typename T>
struct seg_tree {
  T seg[4 * MX], tmp[4 * MX];
  inline T pull(const T& a, const T& b) { return min(a, b); }
  inline void push(int l, int r, int n) {
    seg[n] += tmp[n];
    if (l != r) tmp[n << 1] += tmp[n], tmp[n << 1 | 1] += tmp[n];
    tmp[n] = 0;
  }
  void update(int a, int b, T v, int l = 0, int r = MX - 1, int n = 1) {
    push(l, r, n);
    if (l > r || l > b || r < a) return;
    if (l >= a && r <= b) {
      tmp[n] += v, push(l, r, n);
      return;
    }
    int m = l + r >> 1;
    update(a, b, v, l, m, n << 1), update(a, b, v, m + 1, r, n << 1 | 1);
    seg[n] = pull(seg[n << 1], seg[n << 1 | 1]);
  }
  T query(int a, int b, int l = 0, int r = MX - 1, int n = 1) {
    if (a > b || l > b || r < a) return 1e9;
    push(l, r, n);
    if (l >= a && r <= b) return seg[n];
    int m = l + r >> 1;
    return pull(query(a, b, l, m, n << 1), query(a, b, m + 1, r, n << 1 | 1));
  }
};
int p[MX], pos[MX], a[MX];
seg_tree<ll> seg;
int main() {
  if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    pos[--p[i]] = i;
  }
  for (int i = 0; i < n; ++i) cin >> a[i];
  ll sum = 0;
  for (int i = 0; i < n; ++i) sum += a[i], seg.update(i, i, sum);
  ll ans = seg.query(0, n - 2);
  for (int i = 0; i < n; ++i) {
    seg.update(pos[i], n - 1, -a[pos[i]]);
    seg.update(0, pos[i] - 1, a[pos[i]]);
    ans = min(seg.query(0, n - 2), ans);
  }
  cout << ans;
}
