#include<bits/stdc++.h>
#define M 200005
#define int long long
using namespace std;
int n, to[M];
struct node{
int a, b;
}a[M];
struct tag{
int o;
array<int, 2> s;
};
typedef array<array<int,2>,2> info;
info operator + (const info &x, const info &y)
{
return {min(x[0][0], y[0][0]), min(x[0][1], y[0][1]), min(x[1][0], y[1][0]), min(x[1][1], y[1][1])};
}
tag operator + (tag &x, tag &y)
{
return {x.o ^ y.o, x.s[0] + y.s[x.o], x.s[1] + y.s[x.o ^ 1]};
}
info mulA(info &x, tag &y)
{
int c = y.o;
return {x[c][0] + y.s[c], x[c][1] + y.s[c], x[c ^ 1][0] + y.s[c ^ 1], x[c ^ 1][1] + y.s[c ^ 1]};
}
info mulB(info &x, tag &y)
{
int c = y.o;
return {x[0][c] + y.s[c], x[0][c ^ 1] + y.s[c ^ 1], x[1][c] + y.s[c], x[1][c ^ 1] + y.s[c ^ 1]};
}
struct SGT{
#define ls p << 1
#define rs p << 1 | 1
info tr[M << 2];
tag A[M << 2], B[M << 2];
void tg(int p, tag x)
{
A[p] = A[p] + x;
tr[p] = mulA(tr[p], x);
}
void push_down(int p)
{
tg(ls, A[p]), tg(rs, A[p]);
A[p] = {0, 0, 0};
}
void push_up(int p)
{
B[p] = B[p << 1] + B[p << 1 | 1];
tr[p] = mulB(tr[p << 1], B[p << 1 | 1]) + tr[p << 1 | 1];
}
void build(int l, int r, int p)
{
if(l == r)
{
tr[p] = {1e18, 1e18, 1e18, 1e18};
B[p] = {1, 0, to[l]};
if(!l) tr[p] = {0, 0, 0, 0};
return;
}
int mid = (l + r) >> 1;
build(l, mid, ls);
build(mid + 1, r, rs);
push_up(p);
}
void mul(int l, int r, int ql, int qr, tag x, int p)
{
if(ql <= l && r <= qr) return tg(p, x);
int mid = (l + r) >> 1;
push_down(p);
if(ql <= mid) mul(l, mid, ql, qr, x, ls);
if(qr > mid) mul(mid + 1, r, ql, qr, x, rs);
push_up(p);
}
void update(int l, int r, int x, info I, int p)
{
if(l == r)
{
tr[p] = I;
B[p] = {0, 0, 0};
return;
}
int mid = (l + r) >> 1;
push_down(p);
if(x <= mid) update(l, mid, x, I, ls);
else update(mid + 1, r, x, I, rs);
push_up(p);
}
void query(int l, int r, int ql, int qr, info &I, int p)
{
if(ql <= l && r <= qr)
{
I = mulB(I, B[p]) + tr[p];
return;
}
int mid = (l + r) >> 1;
push_down(p);
if(ql <= mid) query(l, mid, ql, qr, I, ls);
if(qr > mid) query(mid + 1, r, ql, qr, I, rs);
push_up(p);
}
#undef ls
#undef rs
}sgt;
signed main()
{
scanf("%lld", &n);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i].a);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i].b);
sort(a + 1, a + n + 1, [&](node x, node y){
return x.b < y.b;
});
for(int i = 1; i <= n; i++) to[i] = a[i].b, a[i].b = i;
sort(a + 1, a + n + 1, [&](node x, node y){
return x.a < y.a;
});
sgt.build(0, n, 1);
for(int i = 1; i <= n; i++)
{
info I = {1e18, 1e18, 1e18, 1e18};
sgt.query(0, n, 0, a[i].b, I, 1);
sgt.update(0, n, a[i].b, I, 1);
sgt.mul(0, n, 0, a[i].b - 1, {1, 0, a[i].a}, 1);
}
printf("%lld\n", sgt.tr[1][0][0]);
return 0;
}