// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long int ll;

typedef complex<ll> point;



ll cross(point a, point b) { return imag(conj(a) * b);}

ll dot(point a, point b) { return real(conj(a) * b);}

ll area2(point a, point b, point c) { return cross(b-a, c-a);}



namespace std

{

  bool operator< (const point& a, const point& b)

  {

    return real(a) < real(b) || (real(a) == real(b) && imag(a) < imag(b));

  }

}



const ll oo = 1e18;



struct dynamic_hull

{

  dynamic_hull(): hulls() {}

  

  void add_point(point p)

  {

    hull h;

    h.add_point(p);

    

    for(hull& _h: hulls)

      if(_h.empty())

      {

        h.swap(_h);

        break;

      }else h = merge(h, _h), _h.clear();

    

    if(!h.empty()) hulls.emplace_back(h);

  }

  

  ll max_dot(point p)

  {

    ll best = -oo;

    for(hull& h: hulls)

      if(!h.empty()) best = max(best, h.max_dot(p));

    

    return best;

  }

  

private:

  struct hull: vector<point>

  {

    void add_point(point p)

    {

      for(int s=size(); s> 1; --s)

        if(area2(at(s-2), at(s-1), p) < 0) break;

        else pop_back();

      push_back(p);

    }

    

    ll max_dot(point p)

    {

      int lo = 0, hi = (int) size() - 1, mid;

      while(lo < hi)

      {

        mid = (lo + hi) / 2;

        

        if(dot(at(mid), p) <= dot(at(mid + 1), p))

          lo = mid + 1;

        else hi = mid;

      }

    

      return dot(at(lo), p);

    }

  };

  

  static hull merge(const hull& a, const hull& b)

  {

    hull h;

    size_t i = 0, j = 0;

    

    while(i < a.size() && j < b.size())

      if(a[i] < b[j]) h.add_point(a[i++]);

      else h.add_point(b[j++]);

    

    while(i < a.size()) h.add_point(a[i++]);

    while(j < b.size()) h.add_point(b[j++]);

    

    return h;

  }

  

  vector<hull> hulls;

};



const int N = 2e5+7;



dynamic_hull ft[N];



void upd(int x, point p)

{

  for(;x < N; x += x&-x)

    ft[x].add_point(p);

}



ll qry(int x, point p)

{

  ll res = 0;

  for(;x;x-=x&-x)

    res = max(res, ft[x].max_dot(p));

  return res;

}



int main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(0), cout.tie(0);

  

  int n; cin >> n;

  

  vector<ll> h(n), w(n);

  

  for(ll& i: h) cin >> i;

  for(ll& i: w) cin >> i;

  

  

  ll ans = w[0];

  

  upd(h[0], point(w[0], 1));

  

  for(int i=1;i<n;++i)

  {

    auto p = point(1, w[i]);

    

    ll q = qry(h[i], p);

    if(!q) ans = max(ans, w[i]), q = w[i];

    else ans = max(ans, q);

    

    upd(h[i], point(q, 1));

    //~ cerr << q <<  ' ' << ans << endl;

  }

  

  cout << ans << endl;

  

  return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef long long int ll;



const int N = 2e5+7;



ll ft[N];



void upd(int x, ll p)

{

  for(;x < N; x += x&-x)

    ft[x] = max(ft[x], p);

}



ll qry(int x)

{

  ll res = 0;

  for(;x;x-=x&-x)

    res = max(res, ft[x]);

  return res;

}



int main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(0), cout.tie(0);

  

  int n; cin >> n;

  

  vector<ll> h(n), w(n);

  

  for(ll& i: h) cin >> i;

  for(ll& i: w) cin >> i;

  

  

  ll ans = 0;

  



  for(int i=0;i<n;++i)

  {

    ll q = qry(h[i]), tmp;

    ans = max(ans, tmp = q + w[i]);

    upd(h[i], tmp);

    //~ for(int j=1;j<10;++j)

      //~ cerr << setw(2) << ft[j] << ' ';

    //~ cerr << endl;

  }

  

  cout << ans << endl;

  

  return 0;

}


// Fastest known solution for this problem: