#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
  cerr << sdbg << " = " << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << " = " << h << ",";
  _dbg(sdbg + 1, t...);
}
const int N = 2e5 + 5;
const int64_t LINF = 1e18;
int n, a[N], cost[N];
int pos[N];
int64_t st[4 * N], lz[4 * N];
void PushDown(int id, int l, int r) {
  st[id] += lz[id];
  if (l != r) {
    lz[id * 2] += lz[id];
    lz[id * 2 + 1] += lz[id];
  }
  lz[id] = 0;
}
void Update(int id, int l, int r, int i, int j, int64_t val) {
  PushDown(id, l, r);
  if (r < i || l > j) {
    return;
  }
  if (i <= l && r <= j) {
    lz[id] = val;
    PushDown(id, l, r);
    return;
  }
  Update(id * 2, l, (l + r) / 2, i, j, val);
  Update(id * 2 + 1, (l + r) / 2 + 1, r, i, j, val);
  st[id] = min(st[id * 2], st[id * 2 + 1]);
}
void Solve() {
  int64_t ans = LINF;
  Update(1, 1, n, n, n, LINF);
  int64_t sum = 0;
  for (int k = 1; k < n; ++k) {
    sum += cost[k];
    Update(1, 1, n, k, k, sum);
    ans = min(ans, sum);
  }
  for (int i = 1; i <= n; ++i) {
    pos[a[i]] = i;
  }
  for (int minB = 2; minB <= n + 1; ++minB) {
    int p = pos[minB - 1];
    Update(1, 1, n, p, n - 1, -cost[p]);
    Update(1, 1, n, 1, p - 1, cost[p]);
    PushDown(1, 1, n);
    ans = min(ans, st[1]);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> cost[i];
  }
  Solve();
  return 0;
}
