#include "iostream"
#include "fstream"
#include "algorithm"
#include "iomanip"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "map"
#include "set"
#include "list"
#include "deque"
#include "complex"
#include "bitset"
#include "cmath"
#include "unordered_set"
#include "unordered_map"
#include "iterator"
#include <ctime>
#include <cassert>
#include "numeric"
#include <cstdio>

using namespace std;

//#define f(i,n) for(int i=0; i<n;i++) 
//#define rf(i, n) for(int i=n-1;i>=0;i--)
//#define forn(i, j, n) for(int i=j; i<n; i++)
//#define ff(i, j, n) f(i, n)f(j, n)
#define pb push_back
#define maxi(a,b) a = max(a, b);
#define mini(a,b) a = min(a, b);
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sqr(x) ((x) * (x))
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
//typedef long long ll;
typedef long double ld;
#define int long long
#define double ld
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<char> vc;

template<class T>
void show(const vector<T> &a)
{
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

const int sze = 3e5 + 50, oo = 1e18 + 500, mod = 1e9 + 7;
const double eps = 1e-9, PI = 2 * acos(0.0);
vi vertices[sze];
vi visit(sze, 0);
vc used(sze, false);
vi arr(sze, 0);
int n, m, k;
int A, B;
int cnt = 0;

//int dp[10010][110];
//int dpe[10010][10][110];

int dp[sze];
int t[sze];

void add(int ind, int val)
{
	for (int i = ind; i <= n; i+= i&-i)
	{
		maxi(t[i], val);
	}
}

int get(int ind)
{
	cnt = 0;
	for (int i = ind; i; i -= i&-i)
	{
		maxi(cnt, t[i]);
	}
	return cnt;
}

int col[sze];

signed main()
{
	ios::sync_with_stdio(0);
	cout.tie(0); cin.tie(0);
	/*freopen("stars.in", "r", stdin);
	freopen("stars.out", "w", stdout);*/

	/*string s;
	cin >> s >> m;
	n = SZ(s);

	for (int i = 1; i <= 9; i++)
	{
	dp[1][i % m] ++;
	}

	for (int i = 2; i < n; i++)
	{
	for (int j = 0; j < m; j++)
	{
	for (int z = 0; z <= 9; z++)
	{
	dp[i][(j + z) % m] = (dp[i - 1][j] + dp[i][(j + z) % m]) % mod;
	}
	}
	}

	int ans = 0;
	for (int i = 1; i < n; i++)
	{
	ans = (ans + dp[i][0]) % mod;
	}

	for (int i = 1; i <= s[0] - '0'; i++)
	{
	dpe[0][i][i % m]++;
	}

	for (int i = 1; i < n; i++)
	{
	for (int j = 0; j <= 9; j++)
	{
	for (int z = 0; z < m; z++)
	{
	if (j == s[i - 1] - '0')
	{
	for (int a = 0; a <= s[i] - '0'; a++)
	{
	dpe[i][a][(z + a) % m] = (dpe[i][a][(z + a) % m] + dpe[i-1][j][z]) % mod;
	}
	}
	else
	{
	for (int a = 0; a <= 9; a++)
	{
	dpe[i][a][(z + a) % m] = (dpe[i][a][(z + a) % m] + dpe[i - 1][j][z]) % mod;
	}
	}
	}
	}
	}

	for (int i = 0; i <= 9; i ++)
	{
	ans = (ans + dpe[n - 1][i][0]) % mod;
	}

	cout << ans;*/

	cin >> n;
	vi h(n), a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> h[i];
	}

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	int mx = 0;
	for (int i = 0; i < n; i++)
	{
		int x = get(h[i] - 1);
		if (col[h[i]] < x + a[i])
		{
			col[h[i]] = x + a[i];
			maxi(mx, x + a[i]);
			add(h[i], x + a[i]);
		}
	}
	
	cout << mx;
	return 0;
}

Measured memory_bytes: 19968