#include <bits/stdc++.h>
using namespace std;
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char* s) { return to_string((string)s); }
string to_string(const char c) {
  string s = "'";
  s += c;
  s += "'";
  return s;
}
template <class T, class U>
string to_string(const pair<T, U>& p) {
  string s = "(";
  s += to_string(p.first);
  s += ", ";
  s += to_string(p.second);
  s += ")";
  return s;
}
template <class T>
string to_string(const T& a) {
  bool first = true;
  string s = "{";
  for (const auto& x : a) {
    if (!first) {
      s += ", ";
    }
    first = false;
    s += to_string(x);
  }
  s += "}";
  return s;
}
void debug_out() { cerr << '\n'; }
template <class Head, class... Tail>
void debug_out(Head head, Tail... tail) {
  cerr << ' ' << to_string(head);
  debug_out(tail...);
}
using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int inf = 1e9 + 100;
const ll inf64 = 7e18l;
vector<ll> tree;
vector<ll> pref;
vector<ll> lazy;
void push(int t) {
  tree[2 * t] += lazy[t];
  tree[2 * t + 1] += lazy[t];
  lazy[2 * t] += lazy[t];
  lazy[2 * t + 1] += lazy[t];
  lazy[t] = 0;
}
void build(int t, int nl, int nr) {
  if (nl == nr) {
    tree[t] = pref[nl];
    return;
  }
  int mid = nl + (nr - nl) / 2;
  build(2 * t, nl, mid);
  build(2 * t + 1, mid + 1, nr);
  tree[t] = min(tree[2 * t], tree[2 * t + 1]);
}
void update(int t, int nl, int nr, int l, int r, ll val) {
  if (nl > r || nr < l) {
    return;
  }
  if (nl >= l && nr <= r) {
    tree[t] += val;
    lazy[t] += val;
    return;
  }
  push(t);
  int mid = nl + (nr - nl) / 2;
  update(2 * t, nl, mid, l, r, val);
  update(2 * t + 1, mid + 1, nr, l, r, val);
  tree[t] = min(tree[2 * t], tree[2 * t + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  tree.resize(4 * (n + 1));
  lazy.resize(4 * (n + 1));
  pref.resize(n + 1);
  vector<ll> a(n), cost(n);
  vector<int> ind(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ind[a[i]] = i;
  }
  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
  }
  for (int i = 0; i < n; ++i) {
    pref[i + 1] = pref[i] + cost[ind[i + 1]];
  }
  build(1, 0, n);
  assert(tree[1] >= 0);
  ll answer = inf64;
  for (int i = 0; i < n; ++i) {
    update(1, 0, n, 0, a[i] - 1, cost[i]);
    update(1, 0, n, a[i], n, -cost[i]);
    assert(tree[1] >= 0);
    if (i != n - 1) {
      answer = min(answer, tree[1]);
    }
  }
  cout << answer << '\n';
  return 0;
}
