/////////////////////////////////////////////////////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'               `$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//  
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$'                   `$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$'`$$$$$$$$$$$$$'`$$$$$$!                       !$$$$$$'`$$$$$$$$$$$$$'`$$$//
//$$$$  $$$$$$$$$$$  $$$$$$$                         $$$$$$$  $$$$$$$$$$$  $$$$//
//$$$$. `$' \' \$`  $$$$$$$!       $$        $$      !$$$$$$$  '$/ `/ `$' .$$$$//
//$$$$$. !\  i  i .$$$$$$$$       $$$$      $$$$      $$$$$$$$. i  i  /! .$$$$$//
//$$$$$$   `--`--.$$$$$$$$$        $$        $$       $$$$$$$$$.--'--'   $$$$$$//
//$$$$$$L        `$$$$$^^$$                           $$^^$$$$$'        J$$$$$$//
//$$$$$$$.   .'   ""~   $$$    $.                 .$  $$$   ~""   `.   .$$$$$$$//
//$$$$$$$$.  ;      .e$$$$$!    $$.             .$$  !$$$$$e,      ;  .$$$$$$$$//
//$$$$$$$$$   `.$$$$$$$$$$$$     $$$.         .$$$   $$$$$$$$$$$$.'   $$$$$$$$$//
//$$$$$$$$    .$$$$$$$$$$$$$!     $$`$$$$$$$$'$$    !$$$$$$$$$$$$$.    $$$$$$$$//
//$$$$$$$     $$$$$$$$$$$$$$$$.    $    $$    $   .$$$$$$$$$$$$$$$$     $$$$$$$//
//                                 $    $$    $								   //
//                                 $.   $$   .$								   //
//                                `$        $'								   //
//                                 `$$$$$$$$'								   //
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

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

// Type-Define 
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef std::vector<char> vc;
typedef std::vector< vector<int> > vii;
typedef std::pair<int, int> pii;

// Hash-Define
#define int long long
#define MOD 1000000007
#define debugStart cerr << "\n[DEBUG START]\n"
#define debugStop cerr << "\n[DEBUG STOP]\n"
#define KHATAM cout << std::endl;
#define set_zero(x) memset(x, 0, sizeof(x));
#define ForPrint(x) std::for_each( begin(x), end(x), []( auto const& el ){ std::cout << el << " ";  } )

//Functions 
template<typename T> auto gcd ( T a, T b ) -> int  { return (b==0)?a:gcd(b,a%b); }

const int inf = 1e18L + 5;


signed
main() {

    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1; 
    for ( int testcase = 1; testcase <= T; ++testcase ) {

        int N;
        std::cin >> N;
        int arr[N];
        int height[N];
        for ( int i = 0; i < N; ++i  ) {
            std::cin >> height[i];
        //    --height[i];
        }for ( int i = 0; i < N; ++i ) {
            std::cin >> arr[i];
        } 
       int size = N + 1; 
        //dp[i] = maximum profit at height i
        int dp[size];
        int tree[2*size];
        memset(dp, 0, sizeof(dp));
        memset(tree, 0, sizeof(tree));
        auto get_max = [&tree, &size]( int left, int right ) -> int {
            int max_value = -inf;
            for ( left += size, right += size; left < right; left>>=1, right>>=1 ) {
                if(left&1) max_value = max(tree[left++], max_value);
                if(right&1) max_value = max(tree[--right], max_value);
            }
            return max_value;
        };
        auto Add = [&tree, &size]( int pos, int value ) {
            for ( tree[pos+=size] = value; pos > 1; pos>>=1  ) {
                tree[pos>>1] = max( tree[pos], tree[pos^1] );
            }
        };
        for ( int i = 0; i < N; ++i ) {
            
            int max_value = get_max(0, height[i]); // [)
            dp[height[i]] = max(max_value + arr[i], dp[height[i]]);
            Add( height[i], dp[height[i]] ); 
        }
        int answer = tree[1];
        std::cout << tree[1]; 
        KHATAM; 
    }
    return 0;
}


Measured memory_bytes: 8064