#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;
main() {
  int n;
  cin>>n;
  int h[n+1];
  for(int i=1;i<=n;i++){
    cin>>h[i];
  }
  int c[n+1];
  for(int i=1;i<=n;i++){
    cin>>c[i];
  }
  int r[n+1],l[n+1];
  stack<int>q;
  for(int i=n;i>=1;i--){
    while(!q.empty() and h[q.top()]<h[i]){
      q.pop();
    }
    if(!q.empty()){
      r[i]=q.top()-1;
    }else{
      r[i]=n;
    }
    q.push(i);
  }
  stack<int>q2;
  set<pair<int,int>>t;
  multiset<int>mns;
  int dp[n+1]={};
  if(n<=5000){
  for(int i=1;i<=n;i++){
    dp[i]=LLONG_MAX;
    int mx2=0;
    for(int j=i;j>=1;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];
    return 0;
  }
  for(int i=1;i<=n;i++){
    while(!q2.empty() and h[q2.top()]<h[i]){
      q2.pop();
    }
    if(q2.empty()){
      l[i]=1;
    }else{
      l[i]=q2.top()+1;
    }
    q2.push(i);
    t.insert({r[i],dp[i-1]+c[i]});
    mns.insert(dp[i-1]+c[i]);
    while(!t.empty() and (*t.begin()).first<i){
      mns.erase(mns.find((*t.begin()).second));
      t.erase(t.begin());
    }
    dp[i]=min(*mns.begin(),dp[l[i]-1]+c[i]);
    int mx2=0;
    for(int j=i;j>=max(i-1000LL,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];
}
