// KALAM
# include <bits/stdc++.h>

using namespace std;

const int N = 200000 + 77 , Mod = 1000000007;
int n , h[N] , a[N];
long long Fen[N] , A;
inline void Upd(int x , long long val){
   for(;x < N;x += x & - x)
      Fen[x] = max(Fen[x] , val);
}
inline long long Get(int x){
   long long res = 0;
   for(;x;x -= x & - x)
      res = max(res , Fen[x]);
   return res;
}
int main(){
   ios::sync_with_stdio(0);cin.tie(0);
   cin >> n;
   for(int i = 1;i <= n;++ i)
      cin >> h[i];
   for(int i = 1;i <= n;++ i)
      cin >> a[i];
   for(int i = 1;i <= n;++ i){
      long long t = Get(h[i]);
      A = max(A , t + a[i]);
      Upd(h[i] , t + a[i]);
   }
   cout << A;
   return 0;
}
