#include <bits/stdc++.h>

using namespace std;

int n;
int h[200002], a[200002];
long long aib[200002];

void update (int x, long long val) {
  for (int i = x; i <= n; i += (i & (-i)))
    aib[i] = max(aib[i], val);
}

long long query (int x) {
  long long ans = 0;
  for (int i = x; i > 0; i -= (i & (-i)))
    ans = max(ans, aib[i]);
  return ans;
}

int main()
{
  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 x = query(h[i]) + a[i];
    update(h[i], x);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, aib[i]);
  cout << ans;
  return 0;
}
