
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

long long f(long long l,long long r, vector<long long> h, vector<long long> c){
    long long ma = -1,index;
    for(long long i = l;i <= r;i++){
        if(h[i] > ma){
            ma = h[i];
            index = i;
        }
    }
    if(index == l or index == r){
        return c[index];
    }else{
        return c[index] + max(f(l,index - 1, h, c), f(index + 1,r, h, c));
    }
}
int main()
{
    long long n;
    cin >> n;
    vector<long long> h(n);
    vector<long long> c(n);
    for(long long i = 0;i < n;i++){
        cin >> h[i];
    }
    for(long long i = 0;i < n;i++){
        cin >> c[i];
    }
    cout << f(0,n - 1, h, c);
}