#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int f = 0;
  cerr << '{';
  for (auto& i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int M = 1e9 + 7;
struct snode {
  long long val;
  long long delta;
  snode(long long x) {
    val = x;
    delta = 0;
  }
  long long getVal() { return val + delta; }
};
class stree {
 public:
  vector<long long> L, R, A;
  vector<snode> ST;
  int n;
  stree(vector<long long>& a) {
    A = a;
    n = a.size();
    L.resize(4 * n);
    R.resize(4 * n);
    ST = vector<snode>(4 * n, snode(1e18));
  }
  void init(int i, int l, int r) {
    L[i] = l;
    R[i] = r;
    if (l == r) {
      ST[i] = snode(A[l]);
      return;
    }
    long long mid = (l + r) / 2;
    init(2 * i, l, mid);
    init(2 * i + 1, mid + 1, r);
    merge(i);
  }
  void propogate(int i) {
    ST[2 * i].delta += ST[i].delta;
    ST[2 * i + 1].delta += ST[i].delta;
  }
  void merge(int i) {
    ST[i].val = min(ST[2 * i].getVal(), ST[2 * i + 1].getVal());
    ST[i].delta = 0;
  }
  void update(int i, int l, int r, long long x) {
    if (R[i] < l || r < L[i]) {
      return;
    }
    if (l <= L[i] && R[i] <= r) {
      ST[i].delta += x;
      return;
    }
    propogate(i);
    update(2 * i, l, r, x);
    update(2 * i + 1, l, r, x);
    merge(i);
  }
  long long getmin(int i, int l, int r) {
    if (R[i] < l || r < L[i]) {
      return 1e18;
    }
    if (l <= L[i] && R[i] <= r) {
      return ST[i].getVal();
    }
    propogate(i);
    long long left = getmin(2 * i, l, r);
    long long right = getmin(2 * i + 1, l, r);
    merge(i);
    return min(left, right);
  }
  long long findminind(int i, long long x) {
    if (L[i] == R[i]) {
      return L[i];
    }
    if (getmin(2 * i, L[2 * i], R[2 * i]) == x) {
      return findminind(2 * i, x);
    }
    return findminind(2 * i + 1, x);
  }
};
void solve() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<int> inds(n);
  vector<int> C(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
    --A[i];
    inds[A[i]] = i;
  }
  vector<long long> pref(n);
  for (int i = 0; i < n; ++i) {
    cin >> C[i];
    pref[i] = C[i];
    if (i) {
      pref[i] += pref[i - 1];
    }
  }
  stree st(pref);
  st.init(1, 0, n - 1);
  long long ans = pref[0];
  for (int val = 0; val < n; ++val) {
    st.update(1, 0, inds[val] - 1, C[inds[val]]);
    st.update(1, inds[val], n - 1, -C[inds[val]]);
    ans = min(ans, st.getmin(1, 0, n - 2));
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
