#include <bits/stdc++.h>
using namespace std;
long long int n, m, ar[1000005], sum, arr[1000005], tut[1000005];
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> ar[i];
  for (int i = 1; i <= n; i++) tut[ar[i]] = i;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) sum += arr[i];
  for (int i = 1; i <= n - 1; i++)
    if (tut[i] == tut[i + 1] - 1) sum += arr[i];
  cout << sum;
}
