#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define gcd __gcd
#define endl "\n"
#define ld long double
#define int ll
//#pragma optimize(Ofast)
const int mod = 998244353;
int sp[20][200005];
int logs[200005];
int get(int l,int r){
  if(l>r){
    return 0;
  }
  int u=logs[r-l+1];
  return max(sp[u][l],sp[u][r-(1<<u)+1]);
}
main() {
  int n;
  cin>>n;
  int h[n+1];
  logs[0]=-1;
  for(int i=1;i<=n;i++){
    cin>>h[i];
    logs[i]=logs[i/2]+1;
    sp[0][i]=h[i];
  }
  for(int j=1;j<20;j++){
    for(int i=1;i+(1<<j)-1<=n;i++){
      sp[j][i]=max(sp[j-1][i],sp[j-1][i+(1<<(j-1))]);
    }
  }
  int c[n+1];
  for(int i=1;i<=n;i++){
    cin>>c[i];
  }
  int dp[n+1]={};
  for(int i=1;i<=n;i++){
    int mx=get(min(i,200LL)+1,i);
    for(int j=min(i,200LL);j>=1;j--){
      mx=max(mx,h[j]);
      if(mx==h[j]){
        dp[i]=min(dp[i],c[j]+dp[j-1]);
      }
      if(mx==h[i]){
        dp[i]=min(dp[i],c[i]+dp[j-1]);
      }
    }
    dp[i]=LLONG_MAX;
    int mx2=0;
    for(int j=i;j>=max(i-200LL,1LL);j--){
      mx2=max(mx2,h[j]);
      if(mx2==h[i]){
        dp[i]=min(dp[i],c[i]+dp[j-1]);
      }
      if(mx2==h[j]){
        dp[i]=min(dp[i],c[j]+dp[j-1]);
      }
    }
  }
  cout<<dp[n];
}
