#include <bits/stdc++.h>
using namespace std;
const long long inf = 10000000000000000;
long long tre[800005], lazy[800005];
long long cost[800005], cum_cost[800005];
int id[800005];
void propagate(int node, int st, int en) {
  tre[node] += lazy[node];
  if (st != en) {
    lazy[node * 2] += lazy[node];
    lazy[node * 2 + 1] += lazy[node];
  }
  lazy[node] = 0;
  return;
}
void update(int node, int st, int en, int l, int r, long long val) {
  propagate(node, st, en);
  if (st >= l && en <= r) {
    if (st != en) lazy[node * 2] += val, lazy[node * 2 + 1] += val;
    tre[node] = tre[node] + val;
    return;
  }
  if (l > en || r < st) return;
  int mid = (st + en) / 2;
  update(node * 2, st, mid, l, r, val);
  update(node * 2 + 1, mid + 1, en, l, r, val);
  tre[node] = min(tre[node * 2], tre[node * 2 + 1]);
  return;
}
long long query(int node, int st, int en, int l, int r) {
  propagate(node, st, en);
  if (st >= l && en <= r) return tre[node];
  if (st > r || en < l) return inf;
  int mid = (st + en) / 2;
  long long ret = query(node * 2, st, mid, l, r);
  ret = min(ret, query(node * 2 + 1, mid + 1, en, l, r));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    id[i] = a;
  }
  for (int i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    cost[id[i]] = x;
  }
  for (int i = 1; i <= n; i++) cum_cost[i] = cost[i] + cum_cost[i - 1];
  for (int i = 0; i <= n; i++) {
    long long upd = cum_cost[i];
    if (id[1] > i)
      upd += cost[id[1]];
    else
      upd -= cost[id[1]];
    update(1, 0, n, i, i, upd);
  }
  long long ans = query(1, 0, n, 0, n);
  for (int i = 2; i < n; i++) {
    int pos = id[i];
    long long val = cost[pos];
    update(1, 0, n, 0, pos - 1, val);
    update(1, 0, n, pos, n, -1LL * val);
    ans = min(ans, query(1, 0, n, 0, n));
  }
  cout << ans << endl;
}
