#include<bits/stdc++.h>
#include<map>
#include<string>
#define ll          long long
#define pb          push_back
#define mp          make_pair
#define pll         pair<ll,ll>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rep1(i,a,b) for(ll i=a;i<= b;i++)
#define PI          3.14159
#define MAX         1000001
#define left (2*i+1)
#define right (2*i+2)
#define fast_io ios_base::sync_with_stdio(false);  cin.tie(0);  cout.tie(0);
using namespace std;
ll tree[800001];
ll lazy[800001];
/*
void buildtree(ll start,ll end,ll index)
{
  if(start > end)
  {
    return;
  }
  if(start == end)
  {
    tree[index] = arr[start];
    return;
  }
  ll mid = (start + end) >> 1;
  buildtree(start,mid,2*index);
  buildtree(mid + 1,end,2*index + 1);
  tree[index] = max(tree[2*index],tree[2*index + 1]);
  return;
}
*/
void updatelazy(ll start,ll end,ll index,ll val,ll low,ll high)
{
  if(start > end)
  {
    return;
  }
  if(lazy[index] != -1)
  {
    tree[index] = max(tree[index],lazy[index]);
    if(start != end)
    {
      lazy[2*index] = max(lazy[index],lazy[2*index]);
      lazy[2*index + 1] = max(lazy[index],lazy[2*index]);
    }
    lazy[index] = -1;
  }
  if(start > high || end < low)
  {
    return;
  }
  if(start >= low && end <= high)
  {
    tree[index] = val;
    if(start != end)
    {
      lazy[2*index] = max(lazy[index],lazy[2*index]);
      lazy[2*index + 1] = max(lazy[index],lazy[2*index]);
    }
    lazy[index] = -1;
    return;
  }
  ll mid = (start + end) >> 1;
  updatelazy(start,mid,2*index,val,low,high);
  updatelazy(mid + 1,end,2*index + 1,val,low,high);
  tree[index] = max(tree[2*index],tree[2*index + 1]);
  return;
}
ll getval(ll start,ll end,ll index,ll low,ll high)
{
  if(lazy[index] != -1)
  {
    tree[index] = max(tree[index],lazy[index]);
    if(start != end)
    {
      lazy[2*index] = max(lazy[index],lazy[2*index]);
      lazy[2*index + 1] = max(lazy[index],lazy[2*index]);
    }
    lazy[index] = -1;
  }
  if(start > high || end < low)
  {
    return INT_MIN;
  }
  if(start >= low && end <= high)
  {
    tree[index] = max(tree[index],lazy[index]);
    if(start != end)
    {
      lazy[2*index] = max(lazy[index],lazy[2*index]);
      lazy[2*index + 1] = max(lazy[index],lazy[2*index]);
    }
    lazy[index] = -1;
    return tree[index];
  }
  ll mid = (start + end) >> 1;
  return max(getval(start,mid,2*index,low,high),getval(mid + 1,end,2*index + 1,low,high));
}
pll arr[200001];
bool comp(pll p1, pll p2)
{
  if(p1.first == p2.first)
  {
    return p1.second > p2.second;
  }
  return p1.first < p2.first;
}
ll arr1[200001];
int main()
{
  fast_io
  ll n,k;
  cin >> n ;
  memset(lazy,-1,sizeof(lazy));
  rep(i,0,n)
  {
    ll num;
    cin >> num;
    pll p1 = mp(num,i);
    arr[i] = p1;
  }
  rep(i,0,n)
  {
    cin >> arr1[i];
  }
  sort(arr,arr + n,comp);
  ll res = 0;
  rep(i,0,n)
  {
    pll p1 = arr[i];
    ll ans = 0;
    if(p1.second > 0)
    {
      ans = getval(0,n - 1,1,0,p1.second - 1);
    }
    res = max(res,ans + arr1[p1.second]);
    updatelazy(0,n - 1,1,ans + arr1[p1.second],p1.second,p1.second);
  }
  cout << res;
  return 0;
}

Measured memory_bytes: 16640