#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...);
}
struct SegTree {
  long long tree[4 * 200005];
  long long lazy[4 * 200005];
  void push(long long node) {
    tree[2 * node + 1] += lazy[node];
    tree[2 * node + 2] += lazy[node];
    lazy[2 * node + 1] += lazy[node];
    lazy[2 * node + 2] += lazy[node];
    lazy[node] = 0;
  }
  void build(vector<long long> &a, long long node, long long start,
             long long end) {
    if (start == end) {
      tree[node] = a[start];
      return;
    }
    long long mid = (start + end) >> 1;
    build(a, 2 * node + 1, start, mid);
    build(a, 2 * node + 2, mid + 1, end);
    tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
  }
  void rupd(long long node, long long start, long long end, long long l,
            long long r, long long val) {
    if (r < start || l > end) return;
    if (l <= start && end <= r) {
      tree[node] += val;
      lazy[node] += val;
      return;
    }
    push(node);
    long long mid = (start + end) >> 1;
    rupd(2 * node + 1, start, mid, l, r, val);
    rupd(2 * node + 2, mid + 1, end, l, r, val);
    tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);
  }
  long long rquery(long long node, long long start, long long end, long long l,
                   long long r) {
    if (r < start || l > end) return 1000000000000005;
    if (l <= start && end <= r) return tree[node];
    push(node);
    long long mid = (start + end) >> 1;
    long long p1 = rquery(2 * node + 1, start, mid, l, r);
    long long p2 = rquery(2 * node + 2, mid + 1, end, l, r);
    return min(p1, p2);
  }
} st;
void solve() {
  long long n;
  cin >> n;
  vector<long long> p(n);
  vector<long long> rp(n);
  for (long long i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
    rp[p[i]] = i;
  }
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  vector<long long> b(n);
  b[0] = a[0];
  for (long long i = 1; i < n; i++) {
    b[i] = b[i - 1] + a[i];
  }
  st.build(b, 0, 0, n - 1);
  long long ans = st.rquery(0, 0, n - 1, 0, n - 1);
  for (long long i = 0; i < (n); i++) {
    long long pos = rp[i];
    st.rupd(0, 0, n - 1, pos, n - 1, -a[pos]);
    if (pos) st.rupd(0, 0, n - 1, 0, pos - 1, a[pos]);
    ans = min(ans, st.rquery(0, 0, n - 1, 0, n - 2));
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tests;
  tests = 1;
  for (long long i = 0; i < tests; i++) {
    solve();
  }
}
