#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>

using namespace std;

// #define int long long
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<long long, long long>;

#define endl '\n'

#define lol cout << endl

#define all(__ds) __ds.begin(), __ds.end()

#define riter(__it, __ds) for (auto __it = (__ds.rbegin()); __it != (__ds.rend()); __it++)
#define iter(__it, __ds) for (auto __it = (__ds.begin()); __it != (__ds.end()); __it++)

#define rgo(__i, __n) for (int __i = (__n)-1; __i >= 0; __i--)
#define rgo1(__i, __n) for (int __i = (__n); __i >= 1; __i--)
#define go(__i, __n) for (int __i = 0; __i < (__n); __i++)
#define go1(__i, __n) for (int __i = 1; __i <= (__n); __i++)

// Inclusive bounds
#define For(__i, __l, __r) for (int __i = (__l); __i <= (__r); __i++)

#define ins(__p, __n)                     \
    for (int __i = 0; __i < (__n); __i++) \
    cin >> __p[__i]

#define outs(__p, __n)                    \
    for (int __i = 0; __i < (__n); __i++) \
    cout << __p[__i] << " \n"[__i == ((__n)-1)]

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define print(x) cout << (x) << endl
#define whats(x) cout << (#x) << ": " << (x) << endl

template <class T>
void mkMin(T &a, T b)
{
    if (a > b)
        a = b;
}

template <class T>
void mkMax(T &a, T b)
{
    if (a < b)
        a = b;
}

ld binpow(ld base, int pwr)
{
    ld res = 1;

    while (pwr > 0)
    {
        if (pwr & 1)
            res *= base;

        base *= base;

        pwr >>= 1;
    }

    return res;
}

// lower sqrt
template <class T>
T sqrti(T x)
{
    T a = x, b = ((x + 1) >> 1);
    while (a > b)
    {
        a = b;
        b = ((b + (x / b)) >> 1);
    }

    return a;
}

// lower log2
template <class T>
T lgi(T x)
{
    if (x == 0)
        return -1;

    T value = 1;
    int lg = 0;

    while ((value << 1) <= x)
        value <<= 1, lg++;

    return lg;
}

// round up division x/y
template <class T>
inline T ceil(T x, T y)
{
    return (x + y - 1) / y;
}

void Algo(int test)
{
    ll n;
    cin >> n;

    ll h[n];
    ll c[n];
    ins(h, n);
    ins(c, n);

    ll dp[n];

    vector<pll> gr;

    go(i, n)
    {
        while(gr.size() and gr.back().first<h[i])
            gr.pop_back();

        if(gr.empty())
        {
            dp[i] = c[i];
        }
        else
        {
            dp[i] = (gr[0].second ? dp[gr[0].second - 1] : 0) + c[gr[0].second];
            mkMin(dp[i], dp[gr.back().second] + c[i]);
        }

        if(gr.empty() or gr.back().first > h[i]) 
            gr.push_back({h[i], i});

    }

    //     6 6 5 7 5 2
    //     

    // outs(dp, n);

    print(dp[n-1]);
}

/*

DP

which covered
cost 
left / right

cover
be covered

*/

int32_t main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed;

    int tests = 1;
    // cin >> tests;
    for (int test = 1; test <= tests; test++)
    {
        Algo(test);
    }

    return 0;
}