#include <bits/stdc++.h>
using namespace std;
const int MAX = 212345;
struct lazySeg {
  long long seg[MAX * 4], lazy[MAX * 4];
  void update_lazy(int node, int nodeL, int nodeR) {
    if (lazy[node] != 0) {
      seg[node] += lazy[node];
      if (nodeL != nodeR) {
        lazy[node * 2] += lazy[node];
        lazy[node * 2 + 1] += lazy[node];
      }
      lazy[node] = 0;
    }
  }
  long long update(int l, int r, long long value, int node, int nodeL,
                   int nodeR) {
    update_lazy(node, nodeL, nodeR);
    if (r < nodeL || nodeR < l) return seg[node];
    if (l <= nodeL && nodeR <= r) {
      seg[node] += value;
      if (nodeL != nodeR) {
        lazy[node * 2] += value;
        lazy[node * 2 + 1] += value;
      }
      return seg[node];
    }
    int mid = (nodeL + nodeR) / 2;
    return seg[node] = min(update(l, r, value, node * 2, nodeL, mid),
                           update(l, r, value, node * 2 + 1, mid + 1, nodeR));
  }
  long long query(int l, int r, int node, int nodeL, int nodeR) {
    update_lazy(node, nodeL, nodeR);
    if (r < nodeL || nodeR < l) return 1123456789LL * 212345;
    if (l <= nodeL && nodeR <= r) return seg[node];
    int mid = (nodeL + nodeR) / 2;
    return min(query(l, r, node * 2, nodeL, mid),
               query(l, r, node * 2 + 1, mid + 1, nodeR));
  }
} S;
int n, a[MAX], rev_pos[MAX];
long long w[MAX];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    rev_pos[a[i]] = i;
  }
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
    sum += w[i];
  }
  sum -= w[0];
  for (int i = 1; i < n; ++i) {
    S.update(i, i, sum, 1, 1, n - 1);
    sum -= w[i];
  }
  long long ans = S.query(1, n - 1, 1, 1, n - 1);
  for (int k = n; k >= 1; --k) {
    int p = rev_pos[k];
    if (1 <= p) S.update(1, p, -w[p], 1, 1, n - 1);
    if (p + 1 <= n - 1) S.update(p + 1, n - 1, w[p], 1, 1, n - 1);
    ans = min(ans, S.query(1, n - 1, 1, 1, n - 1));
  }
  cout << ans;
}
