#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int n;
int a[200002];
long long b[200002];
pair<long long, long long> p[200002];
long long tree[524288], lazy[524288];
void propagation(int node, int S, int E) {
  if (lazy[node] != 0) {
    tree[node] += lazy[node];
    if (S != E) {
      lazy[2 * node] += lazy[node];
      lazy[2 * node + 1] += lazy[node];
    }
    lazy[node] = 0;
  }
}
void upd(int node, int S, int E, int i, int j, long long val) {
  propagation(node, S, E);
  if (i > E || j < S) return;
  if (j >= E && i <= S) {
    tree[node] += val;
    if (S != E) {
      lazy[node * 2] += val;
      lazy[node * 2 + 1] += val;
    }
    return;
  }
  upd(2 * node, S, (S + E) / 2, i, j, val);
  upd(2 * node + 1, (S + E) / 2 + 1, E, i, j, val);
  tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
long long find(int node, int S, int E, int i, int j) {
  propagation(node, S, E);
  if (i > E || j < S) return 987654321987654321;
  if (j >= E && i <= S) return tree[node];
  return min(find(node * 2, S, (S + E) / 2, i, j),
             find(2 * node + 1, (S + E) / 2 + 1, E, i, j));
}
int main() {
  scanf("%d", &n);
  for (int(i) = (1); (i) <= (n); (i) += (1)) scanf("%d", a + i);
  for (int(i) = (1); (i) <= (n); (i) += (1)) scanf("%lld", b + i);
  for (int(i) = (1); (i) <= (n); (i) += (1)) p[i] = make_pair(a[i], b[i]);
  sort(p, p + n + 1);
  long long t2 = 0;
  for (int(i) = (0); (i) <= (n); (i) += (1)) {
    t2 += p[i].second;
    upd(1, 0, n, i, i, t2);
  }
  long long ans = 987654321987654321;
  for (int(i) = (1); (i) <= (n - 1); (i) += (1)) {
    upd(1, 0, n, 0, a[i] - 1, b[i]);
    upd(1, 0, n, a[i], n, -b[i]);
    ans = min(ans, find(1, 0, n, 0, n));
  }
  printf("%lld", ans);
}
