/// __Macro's__
#include <bits/stdc++.h>
using namespace std;
#define ll                   long long
#define ld                   long double
#define II                   pair<int,int>
#define LL                   pair<ll,ll>
#define III                  pair<int, II>
#define F                    first
#define S                    second
#define mkpr                 make_pair
#define VI                   vector<int>
#define VII                  vector<II>
#define VIII                 vector<pair<int, II>>
#define VVI                  vector<vector<int>>
#define VVII                 vector<vector<II>>
#define VL                   vector<ll>
#define VB                   vector<bool>
#define MII                  map<int, int>
#define SI                   set<int>
#define QI                   queue<int>
#define QII                  queue<II>
#define PQ                   priority_queue
#define PQI                  priority_queue<int>
#define tp(v)                v.top(), v.pop()
#define pb                   push_back
#define pf                   push_front
#define Pb                   pop_back
#define Pf                   pop_front
#define fpb                  emplace_back
#define _all(v)              v.begin(), v.end()
#define _sort(v)             sort(_all(v))
#define __L                  p<<1, L, L+R>>1
#define __R                  p<<1|1, (L+R>>1)+1, R
#define Gch()                getchar()
#define LINE                 putc('\n', stdout)
#define SPACE                putc(' ' , stdout)
#define TAB                  putc('\t', stdout)
#define _YES                 printf("YES")
#define _NO                  printf("NO")
#define _Yes                 printf("Yes")
#define _No                  printf("No")
#define _yes                 printf("yes")
#define _no                  printf("no")
#define _0                   printf("0")
#define __1                  printf("-1")
#define _outS(_)             printf("%s",_)
#define _outC(_)             printf("%c",_)
#define _out(_)              printf("%i",_)
#define _Fout(_)             (WriteInt(_))
#define _outLL(_)            printf("%lld",_)
#define _outI64d(_)          printf("%lld",_);
#define __out(_,__)          printf("%i %i",_,__)
#define __Fout(_,__)         (WriteInt(_), SPACE, WriteInt(__))
#define __outLL(_,__)        printf("%lld %lld",_,__)
#define ___out(_,__,___)     printf("%i %i %i",_,__,___)
#define ___Fout(_,__,___)    (WriteInt(_), SPACE, WriteInt(__), SPACE, WriteInt(___))
#define ___outLL(_,__,___)   printf("%lld %lld %lld",_,__,___)
#define _inS(_)              scanf("%s",_)
#define __inS(_, __)         scanf("%s%s",_,__)
#define _inC(_)              scanf("%c",&_)
#define _in(_)               scanf("%i", &_)
#define _Fin(_)              (_=ReadInt())
#define _inLL(_)             scanf("%lld", &_)
#define _inI64d(_)           scanf("%lld",_);
#define __in(_,__)           scanf("%i%i",&_,&__)
#define __Fin(_,__)          (_=ReadInt(), __=ReadInt())
#define __inLL(_,__)         scanf("%lld%lld",&_,&__)
#define ___in(_,__,___)      scanf("%i%i%i",&_,&__,&___)
#define ___Fin(_,__,___)     (_=ReadInt(), __=ReadInt(), ___=ReadInt())
#define ___inLL(_,__,___)    scanf("%lld%lld%lld",&_,&__,&___)
#define _S_(x)               #x
#define msg(x)               printf(_S_(x))
#define debug(x)             printf("--> Debuging : "), printf(_S_(x))
#define fastIO()             ios_base::sync_with_stdio(0);cin.tie(NULL);
#define _memset(_)           memset(_, -1, sizeof _)
#define _for(i, n)           for(int i	=0 ; i<n ; i++)
#define __for(i, j, n)       for(int i=j ; i<n ; i++)
#define FOR(i, condition)    for(int i=0 ; condition ; i++)
#define _rof(i, n)           for(int i=n ; i-- ;    )
#define __rof(i, j, n)       for(int i=n-1 ; i>=j ; i-- )
#define rm_imp(d, p)         (round(d*p)/p)
#define __lcm(a, b)          ((a*b)/__gcd(a,b))
#define _log2(x)             (31-__builtin_clz(x))
#define INF 2e9
#define EPS 1e-9
#define MOD 1000000007
#define VISITED     1
#define UNVISITED  -1
#define EXPLORING   0
#define CYCLE       2
#define HEAD        3
#define END 7
inline int ReadInt(){
  int x=0,f=1;char ch=getchar();
  while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  return x*f;
}
inline void WriteInt(int x){
  char CH[20];
  int Num=0;if(!x){putchar('0');puts("");return;}
  while(x>0)CH[++Num]=x%10,x/=10;
  while(Num)putchar(CH[Num--]+48);
  puts("");
}
int tmp,nn,mm,kk,aa,bb,ty;
///
int n;
II A[200200];
class fenwick_bottomUp_rmxq{
private:
  int n;
  VL fn;
public:
  fenwick_bottomUp_rmxq(int sz){
    n=sz;
    fn.assign(n+2, 0);
  }
  void upd(int i, ll v){
    for(;i<=n;i+=i&-i) fn[i]=max(fn[i], v);
  }
  ll rmxq(int i){
    ll ret=0;
    for(;i;i-=i&-i) ret=max(ret, fn[i]);
    return ret;
  }
};
int main(){
  _in(n);
  fenwick_bottomUp_rmxq fn(n+3);
  _for(i, n) _in(A[i].F);
  _for(i, n) _in(A[i].S);
  ll mx = 0;
  _for(i, n){
    ll m = fn.rmxq(A[i].F);
    mx = max(mx, m+A[i].S);
    fn.upd(A[i].F, m+A[i].S);
  }
  _outLL(mx); LINE;

}


Measured memory_bytes: 3328