#include <bits/stdc++.h>
using namespace std;
template <class T>
struct seglazy {
  vector<pair<long long, long long> > lohi;
  vector<T> values, delta;
  int n;
  long long INF_VALUE = 1e15;

 private:
  void pushDown(int i) {
    delta[2 * i] += delta[i];
    delta[2 * i + 1] += delta[i];
    delta[i] = 0;
  }
  void update(int i) {
    values[i] =
        min(values[2 * i] + delta[2 * i], values[2 * i + 1] + delta[2 * i + 1]);
  }
  void init(int i, int a, int b) {
    lohi[i].first = a;
    lohi[i].second = b;
    if (a >= b) return;
    int m = (a + b) / 2;
    init(2 * i, a, m);
    init(2 * i + 1, m + 1, b);
  }
  T rangeQueryUtil(int i, int a, int b) {
    if (a > b || lohi[i].first > b || lohi[i].second < a)
      return INF_VALUE;
    else if (lohi[i].first >= a && lohi[i].second <= b)
      return values[i] + delta[i];
    else {
      pushDown(i);
      T left = rangeQueryUtil(2 * i, a, b);
      T right = rangeQueryUtil(2 * i + 1, a, b);
      update(i);
      return min(left, right);
    }
  }
  void rangeUpdateUtil(int i, int a, int b, T val) {
    if (a > b || lohi[i].first > b || lohi[i].second < a)
      return;
    else if (lohi[i].first >= a && lohi[i].second <= b)
      delta[i] += val;
    else {
      pushDown(i);
      rangeUpdateUtil(2 * i, a, b, val);
      rangeUpdateUtil(2 * i + 1, a, b, val);
      update(i);
    }
  }

 public:
  seglazy(int n) {
    this->n = n;
    lohi.resize(4 * n + 1);
    delta.resize(4 * n + 1);
    values.resize(4 * n + 1);
    fill(values.begin(), values.end(), 0);
    fill(delta.begin(), delta.end(), 0);
    init(1, 0, n - 1);
  }
  seglazy(vector<T> arr) {
    this->n = arr.size();
    lohi.resize(4 * n + 1);
    delta.resize(4 * n + 1);
    values.resize(4 * n + 1);
    fill(delta.begin(), delta.end(), 0);
    init(1, 0, n - 1);
    for (long long i = 0; i < n; i++) rangeUpdate(i, i, arr[i]);
  }
  void rangeUpdate(int from, int to, T val) {
    rangeUpdateUtil(1, from, to, val);
  }
  T rangeQuery(int from, int to) { return rangeQueryUtil(1, from, to); }
  void print() {
    for (long long i = 0; i < n; i++) cout << this->rangeQuery(i, i) << ' ';
    cout << '\n';
  }
};
const int sz = 2e5 + 5;
long long n, p[sz], a[sz];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> p[i];
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ret = min(a[0], a[n - 1]);
  seglazy<long long> seg(n + 1);
  for (long long i = 0; i < n; i++) {
    seg.rangeUpdate(p[i], n, a[i]);
  }
  for (long long i = 0; i < n; i++) {
    seg.rangeUpdate(p[i], n, -a[i]);
    seg.rangeUpdate(0, p[i] - 1, a[i]);
    if (i != n - 1) ret = min(ret, seg.rangeQuery(0, n));
  }
  cout << ret << '\n';
  return 0;
}
