#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ios_base::sync_with_stdio(false);
	ll n;
	cin>>n;
	ll a[n] = {}, q[n] = {}, m = -1, mi, res = 0;
	for(ll i = 0; i < n; i++){
	   cin>>a[i];
	   if(a[i] > m){
	      m = a[i];
	      mi = i;
	   }
	}
	for(ll i = 0; i < n; i++){
	   cin>>q[i];
	}
	res += q[mi];
	ll d = n;
	while(true){
	   ll m1 = -1, mi1;
	   if(mi < d/2){
	      for(ll i = 0; i < mi; i++){
	         if(m1 < a[i]){
	            m1 = a[i];
	            mi1 = i;
	         }
	      }res += q[mi1];
	   }else{
	      for(ll i = mi+1; i < d; i++){
	         if(m1 < a[i]){
	            m1 = a[i];
	            mi1 = i;
	         }
	      }res += q[mi1];
	   }
	   if(abs(mi1-mi) <= 1) break;
	   d = mi;
	   mi = mi1;
	}
	cout<<res;
}