#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
#define sqrt sqrtl

using namespace std;

const long long oo = 1000000000000000000;

long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;

int pr[ 1000001 ], sf[ 1000001 ], dp[ 1000001 ];
vector <int> v;

main () {
  int n;
  cin >> n;
  int a[ n + 1 ], b[ n + 1 ];
  for ( int i = 1; i <= n; i ++ ) {
    cin >> a[ i ];
  }
  for ( int i = 1; i <= n; i ++ ) {
    cin >> b[ i ];
    while ( v.size() && a[ v.back() ] < a[ i ] )
      v.pop_back();
    if ( v.size() )
      pr[ i ] = v.back();
    v.push_back( i );
  }
  v.clear();
  for ( int i = n; i >= 1; i -- ) {
    while ( v.size() && a[ v.back() ] < a[ i ] )
      v.pop_back();
    if ( v.size() )
      sf[ i ] = v.back();
    v.push_back( i );
    dp[ i ] = oo;
  }
  for ( int i = 1; i <= n; i ++ ) {
    dp[ i ] = min( dp[ i ], dp[ pr[ i ] ] + b[ i ] );
    dp[ sf[ i ] - 1 ] = min( dp[ sf[ i ] - 1 ], dp[ i - 1 ] + b[ i ] );
  }
  cout << dp[ n ];
}
