#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
void read (T& num) {
T res = 0;
T ch = getchar();
T op = 1;
while (!isdigit (ch) && ch != EOF) {
op = (ch == '-' ? -1 : 1);
ch = getchar ();
}
while (isdigit (ch)) {
res = (res * 10) + (ch - '0');
ch = getchar ();
}
num = op * res;
}
class Read {
public:
Read operator>> (auto& other) {
read (other);
return (*this);
}
};
Read in;
constexpr int N = 1919810;
int n;
int c[N];
long long v[N];
long long f[N];
long long sum[N];
int first[N];
int last[N];
long long res;
void solve (int l, int r) {
int m = (l + r) / 2;
int i = m;
int j = m;
queue<int> q;
q.push(c[m]);
bool flag = false;
while (!q.empty()) {
int num = q.front();
q.pop();
if (first[num] < l || r < last[num]) {
flag = true;
break;
}
else {
for (int k = i - 1; k >= first[num]; k--) {
q.push(c[k]);
}
for (int k = j + 1; k <= last[num]; k++) {
q.push(c[k]);
}
i = min (i, first[num]);
j = max (j, last[num]);
}
}
if (!flag) {
long long now = 0;
for (int k = i; k <= j; k++) {
now += v[k] * f[k - i + 1];
}
if (!res || now < res) {
res = now;
}
}
if (l < r) {
solve (l, m);
solve (m + 1, r);
}
}
int main () {
in >> n;
for (int i = 1; i <= n; i++) {
in >> c[i];
if (!first[c[i]]) {
first[c[i]] = i;
}
last[c[i]] = i;
}
for (int i = 1; i <= n; i++) {
in >> v[i];
}
for (int i = 1; i <= n; i++) {
in >> f[i];
f[i] *= f[i];
sum[i] = f[i] + sum[i - 1];
}
solve (1, n);
printf ("%lld\n", res);
return 0;
}