stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Faster C++ solution for the same problem (measured speedup: 6.316935065417172x):
#include <bits/stdc++.h>



using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>





#define f first

#define s second

#define db long double

#define pb push_back

#define sz(c) (c).size()

#define all(c) (c).begin(),(c).end()



typedef long long ll;

typedef pair <int , int> ii;

typedef pair <ii , int> iii;

typedef vector <int> vi;

typedef vector <ii> vii;

typedef vector <iii> viii;

typedef vector <vi> vvi;

typedef vector <vii> vvii;



const ll mod = 1e9+7;

const int MAX = 2e5+5;

const db eps = 1e-13;



ll BIT[MAX];



void update( int x , ll v ) {



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

        BIT[x] = max( BIT[x] , v );

}



ll query ( int x ) {



    ll res = 0;

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

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

    return res;



}



int main()

{

	ios_base::sync_with_stdio(0);

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

    cout<<setprecision(11)<<fixed;



    int n;cin>>n;

    vi H(n+1);

    vi A(n+1);

    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++ ) {



        int h , a;

        h = H[i];

        a = A[i];



        ll best = query( h-1 );



        update( h , best+a );



    }

    cout<<query( n+1 )<<"\n";



}
// Slower C++ solution:
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Faster C++ solution for the same problem (measured speedup: 6.380413126015135x):
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N=200005;

int tr[N<<2],h[N],a[N],n,ans;

void pushup(int k)

{

	tr[k]=max(tr[k<<1],tr[k<<1|1]);

}

void update(int k,int l,int r,int x,int v)

{

	if(l==r)

	{

		tr[k]=v;

		return;

	}

	int mid=l+r>>1;

	if(x<=mid) update(k<<1,l,mid,x,v);

	else update(k<<1|1,mid+1,r,x,v);

	pushup(k);

}

int query(int k,int l,int r,int a,int b)

{

	if(l==a&&r==b)

		return tr[k];

	int mid=l+r>>1;

	if(b<=mid) return query(k<<1,l,mid,a,b);

	else if(a>mid) return query(k<<1|1,mid+1,r,a,b);

	else return max(query(k<<1,l,mid,a,mid),query(k<<1|1,mid+1,r,mid+1,b));

}

main()

{

	ios::sync_with_stdio(false);

	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++)

	{

		int mx=query(1,1,n,1,h[i])+a[i];

		ans=max(ans,mx);

		update(1,1,n,h[i],mx);

	}

	cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Faster C++ solution for the same problem (measured speedup: 6.528107644550599x):
#include <cstdio>

#include <iostream>



using namespace std;



typedef long long ll;

const int MAXN = 200010;



int n, a[MAXN], h[MAXN];

ll f[MAXN];



struct node{

	int l, r;

	ll val;

}t[MAXN<<2];

inline void pushUp(int k){

	t[k].val = max(t[k<<1].val, t[k<<1|1].val);

}

void build(int l, int r, int k)

{

	t[k].l = l; t[k].r = r; t[k].val = 0;

	if(l == r) return ;

	int mid = l+r>>1;

	build(l, mid, k<<1);

	build(mid+1, r, k<<1|1);

	pushUp(k);

}

void update(int x, ll v, int k)

{

	if(t[k].l == x && t[k].r == x){

		t[k].val = max(t[k].val, v);

		return ;

	}

	int mid = t[k].l+t[k].r>>1;

	if(x <= mid) update(x, v, k<<1);

	else update(x, v, k<<1|1);

	pushUp(k);

}

ll query(int x, int y, int k)

{

	if(t[k].l >= x && t[k].r <= y) return t[k].val;

	int mid = t[k].l+t[k].r>>1;

	ll res = 0;

	if(x <= mid) res = max(res, query(x, y, k<<1));

	if(y >= mid+1) res = max(res, query(x, y, k<<1|1));

	return res;

}



int main()

{

	scanf("%d",&n);

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

		scanf("%d",&h[i]);

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

		scanf("%d",&a[i]);

	build(1, n, 1);

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

//		for(int j=1;j<i;j++){

//			if(h[j] < h[i])

//				f[i] = max(f[i], f[j]);

//		}

		if(h[i] > 1) f[i] = query(1, h[i]-1, 1);

		f[i] += a[i];

		update(h[i], f[i], 1);

	}

	ll ans = 0;

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

		ans = max(ans, f[i]);

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>     



//#pragma GCC optimize("Ofast")

//#pragma comment(linker, "/stack:200000000")

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4"



#define mp make_pair

#define pb push_back

#define f first

#define s second

#define ll long long

#define int ll

#define forn(i, a, b) for(int i = (a); i <= (b); ++i)

#define forev(i, b, a) for(int i = (b); i >= (a); --i)

#define VAR(v, i) __typeof( i) v=(i)

#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)

#define all(x) (x).begin(), (x).end()

#define sz(x) ((int)(x).size())

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);

 

using namespace std;

 

const int maxn = (int)2e5+100;

const int mod = (int)1e9 + 7;

 

#define inf mod

 

typedef long double ld;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

typedef vector<int> vi;   

typedef vector<ll> Vll;               

typedef vector<pair<int, int> > vpii;

typedef vector<pair<ll, ll> > vpll;



int n, dp[maxn],a[maxn],b[maxn], t[maxn];

vi g[maxn];

void upd(int pos,int val){

  for(;pos<maxn;pos=(pos|(pos+1)))

    t[pos]=max(t[pos],val);

}

int get(int r){

  int res=0;

  for(;r>=0;r=(r&(r+1))-1)

    res=max(res,t[r]);

  return res;

}

main () {

  cin>>n;

  forn(i,1,n) cin>>a[i];

  forn(i,1,n) cin>>b[i];

  forn(i,1,n){

    dp[i]=get(a[i]-1)+b[i];

    upd(a[i],dp[i]);

  }

  cout<<*max_element(dp+1,dp+n+1);

}

// Faster C++ solution for the same problem (measured speedup: 6.277022742096636x):
#include <bits/stdc++.h>



#define forn(i, n) for (int i = 0; i < int(n); ++i)

#define for1(i, n) for (int i = 1; i <= int(n); ++i)

#define F first

#define S second

#define all(x) (x).begin(),(x).end()

#define sz(x) int(x.size())

#define pb push_back



using namespace std;

using ll=long long;

using pii= pair<int, int>;

mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

template<typename... T> void rd(T&... args) {((cin>>args), ...);}

template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}



struct fenwick{

    int n;

    vector<ll> t;

    fenwick(int n_):n(n_),t(n+1){}

    void update(int i,ll x){

        for(;i<=n;i+=i&-i){

            t[i]=max(t[i],x);

        }

    }

    ll query(int i){

        ll res=0;

        for(;i>0;i-=i&-i) res=max(res,t[i]);

        return res;

    }

};

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int n;

    cin>>n;

    vector<int> h(n),a(n);

    for(auto& it:h) cin>>it;

    for(auto& it:a) cin>>it;

    fenwick tr(n);

    forn(i,n){

        tr.update(h[i],tr.query(h[i])+a[i]);

    }

    cout<<tr.query(n);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;



#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

#define int long long                           //del this if TLE or MLE

#define ll long long

#define pr pair<int,int>

#define mp make_pair

#define pb push_back

#define eb emplace_back

//#define fow(a,b,c) for(int i=a;i<=b;i+=c)

//#define bck(a,b,c) for(int i=a;i>=b;i-=c)

#define h first

#define a second

#define loop int tests;cin>>tests;while(tests--)



const int N=2e5+5;



int f[N];



pr tree[N];



int bit[N];



void update(int x, int val)

{

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

	{

		bit[i]=max(bit[i],val);

	}

}

int get(int x)

{

	int ans=0;

	for(int i=x;i>0;i-=(i&-i))

	{

		ans=max(ans,bit[i]);

	}

	return ans;

}

signed main()

{

	int n;

	cin>>n;

	memset(bit,0,sizeof(bit));

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

		cin>>tree[i].h;

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

	{

		cin>>tree[i].a;

		f[i]=tree[i].a;

	}

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

	{

		f[i]=max(f[i],get(tree[i].h-1)+tree[i].a);

		update(tree[i].h,f[i]);

	}

	int output=0;

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

		output=max(output,f[i]);

	cout<<output<<"\n";

}


// Faster C++ solution for the same problem (measured speedup: 2.6181203008219405x):
#include<bits/stdc++.h>

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 200005

using namespace std;

using LL = long long;

int n,h[M],a[M];

LL dp[M],tree[M];

int low(int x) {

    return x&-x;

}

LL ask(int x) {

    LL res = 0;

    for (int i=x; i>0; i-=low(i)) res = max(res, tree[i]);

    return res;

}

void upd(int x,LL v) {

    for (int i=x; i<=n; i+=low(i)) tree[i] = max(tree[i], v);

}



int main()

{

    scanf("%d", &n);

    REP(i,1,n) scanf("%d", &h[i]);

    REP(i,1,n) scanf("%d", &a[i]);

    

    REP(i,1,n) {

        dp[i] = ask(h[i]-1) + a[i];

        upd(h[i], dp[i]);

    }

    printf("%lld\n", *max_element(dp+1, dp+n+1));

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define int long long

#define endl '\n'

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;



const int N=2e5;

vector<int>B(N+1);

void insert(int I,int val){

	while( I<=N ){

		B[I]=max(B[I],val);

		I+=I&-I;

	}

}

int query(int I){

	int mx=0;

	while( I > 0){

		mx=max(mx,B[I]);

		I-=I&-I;

	}

	return mx;

}



int32_t main()

{

   IOS

   int n;cin>>n;

   vector<int>h(n+1);

   vector<int>b(n+1);

   int ans=0;

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

   cin>>h[i];

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

   cin>>b[i];

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

	   int mx=query(h[i]);

	   insert(h[i],mx+b[i]);

	   ans=max(ans,mx+b[i]);

   }

   cout<< ans << endl;

   

  

  

  

}


// Faster C++ solution for the same problem (measured speedup: 2.6111779898547995x):
#include<bits/stdc++.h>

#define int long long

#define endl "\n"

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;





const int maxn = 2e5 + 10;

int B[maxn],h[maxn],a[maxn];

int n;

void update(int I,int val)

{

	while(I &&  I < maxn)

	{

		B[I]=max(B[I],val);

		I+=I&-I;

	}

}

int query(int I)

{

	int mx=0;

	while( I > 0 )

	{

		mx=max(mx,B[I]);

		I-=I&-I;

	}

	return mx;

}



int32_t main()

{

	IOS

	cin >> n;

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

	cin>>h[i];

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

	cin>>a[i];

	

	int ans = 0;

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

	{

		int mx = query(h[i]);

		update(h[i],mx+a[i]);

		ans=max(ans,mx+a[i]);

	}

	

	cout << ans << endl;

	

	

}
// Slower C++ solution:
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 1 << 18;



ll t[2*N];

void upd(int x, ll v) {

  t[x+N] = v;

  for (x += N; x > 1; x /= 2) {

    t[x/2] = max(t[x], t[x^1]);

  }

}

ll query(int l, int r) {

  ll ans = 0;

  for (l += N, r += N+1; l < r; l /= 2, r /= 2) {

    if (l%2) ans = max(ans, t[l++]);

    if (r%2) ans = max(ans, t[--r]);

  }

  return ans;

}



int n;

int h[N];

int a[N];

ll dp[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  cin >> n;

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

    cin >> h[i];

  }

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

    cin >> a[i];

  }

  dp[0] = a[0];

  upd(h[0], dp[0]);

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

    dp[i] = query(0, h[i]) + a[i];

    upd(h[i], dp[i]);

  }

  cout << *max_element(dp, dp + N) << nl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.8047695007200373x):
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 2e5+10;

inline char get() {

	static char buf[10000], *S = buf, *T = buf;

	if (S == T) {

		T = (S = buf) + fread(buf, 1, 10000, stdin);

		if (S == T) return EOF;

	}

	return *S++;

}

inline void read(int &x) {

	static char c; x = 0; int sgn = 0;

	for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;

	for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';

	if (sgn) x = -x;

}



ll t[2*N];

void upd(int x, ll v) {

  t[x+N] = v;

  for (x += N; x > 1; x /= 2) {

    t[x/2] = max(t[x], t[x^1]);

  }

}

ll query(int l, int r) {

  ll ans = 0;

  for (l += N, r += N+1; l < r; l /= 2, r /= 2) {

    if (l%2) ans = max(ans, t[l++]);

    if (r%2) ans = max(ans, t[--r]);

  }

  return ans;

}



int n;

int h[N];

int a[N];

ll dp[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  read(n);

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

    read(h[i]);

  }

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

    read(a[i]);

  }

  dp[0] = a[0];

  upd(h[0], dp[0]);

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

    dp[i] = query(0, h[i]) + a[i];

    upd(h[i], dp[i]);

  }

  cout << *max_element(dp, dp + n) << nl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 1 << 18;



ll t[2*N];

void upd(int x, ll v) {

  t[x+N] = v;

  for (x += N; x > 1; x /= 2) {

    t[x/2] = max(t[x], t[x^1]);

  }

}

ll query(int l, int r) {

  ll ans = 0;

  for (l += N, r += N+1; l < r; l /= 2, r /= 2) {

    if (l%2) ans = max(ans, t[l++]);

    if (r%2) ans = max(ans, t[--r]);

  }

  return ans;

}



int n;

int h[N];

int a[N];

ll dp[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  cin >> n;

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

    cin >> h[i];

  }

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

    cin >> a[i];

  }

  dp[0] = a[0];

  upd(h[0], dp[0]);

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

    dp[i] = query(0, h[i]) + a[i];

    upd(h[i], dp[i]);

  }

  cout << *max_element(dp, dp + N) << nl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.8072218432153733x):
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 2e5+10;

inline char get() {

	static char buf[10000], *S = buf, *T = buf;

	if (S == T) {

		T = (S = buf) + fread(buf, 1, 10000, stdin);

		if (S == T) return EOF;

	}

	return *S++;

}

inline void read(int &x) {

	static char c; x = 0; int sgn = 0;

	for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;

	for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';

	if (sgn) x = -x;

}



ll t[N+1];

void upd(int x, ll v) {

  for (; x <= N; x += (x & -x)) t[x] = max(t[x], v);

}

ll query(int x) {

  ll ans = 0;

  for (; x; x -= (x & -x)) ans = max(ans, t[x]);

  return ans;

}



int n;

int h[N];

int a[N];

ll dp[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  read(n);

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

    read(h[i]);

  }

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

    read(a[i]);

  }

  dp[0] = a[0];

  upd(h[0], dp[0]);

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

    dp[i] = query(h[i]) + a[i];

    upd(h[i], dp[i]);

  }

  cout << *max_element(dp, dp + n) << nl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 1 << 18;



ll t[2*N];

void upd(int x, ll v) {

  t[x+N] = v;

  for (x += N; x > 1; x /= 2) {

    t[x/2] = max(t[x], t[x^1]);

  }

}

ll query(int l, int r) {

  ll ans = 0;

  for (l += N, r += N+1; l < r; l /= 2, r /= 2) {

    if (l%2) ans = max(ans, t[l++]);

    if (r%2) ans = max(ans, t[--r]);

  }

  return ans;

}



int n;

int h[N];

int a[N];

ll dp[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  cin >> n;

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

    cin >> h[i];

  }

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

    cin >> a[i];

  }

  dp[0] = a[0];

  upd(h[0], dp[0]);

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

    dp[i] = query(0, h[i]) + a[i];

    upd(h[i], dp[i]);

  }

  cout << *max_element(dp, dp + N) << nl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.8067050878727047x):
#include <bits/stdc++.h>

const char nl = '\n';

using namespace std;

typedef long long ll;

typedef long double ld;

typedef complex<ld> pt;

const int MOD = 1e9 + 7;

const int INF = 0x3f3f3f3f;

const int N = 2e5+10;

inline char get() {

	static char buf[10000], *S = buf, *T = buf;

	if (S == T) {

		T = (S = buf) + fread(buf, 1, 10000, stdin);

		if (S == T) return EOF;

	}

	return *S++;

}

inline void read(int &x) {

	static char c; x = 0; int sgn = 0;

	for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;

	for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';

	if (sgn) x = -x;

}



ll t[N+1];

void upd(int x, ll v) {

  for (; x <= N; x += (x & -x)) t[x] = max(t[x], v);

}

ll query(int x) {

  ll ans = 0;

  for (; x; x -= (x & -x)) ans = max(ans, t[x]);

  return ans;

}



int n;

int h[N];

int a[N];



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);

  read(n);

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

    read(h[i]);

  }

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

    read(a[i]);

  }

  ll ans = a[0];

  upd(h[0], a[0]);

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

    ll dp = query(h[i]) + a[i];

    ans = max(ans, dp);

    upd(h[i], dp);

  }

  cout << ans << nl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <iostream>

#include <string>

#include <algorithm> 

#include <cmath>

#include <cstdlib>

using namespace std;

#define ll long long 

#define gc getchar_unlocked

const ll int INF = 1e18;

const int MOD = 1000000007;



const int MAXN = 200001;

ll tree[4*MAXN];

ll h[MAXN];

ll a[MAXN];

ll dp[MAXN];



void build(int i,int l,int r)

{

    if(l == r)

    {

        tree[i] = dp[l]; return;

    }

    int mid = (l+r)/2; 

    build(2*i,l,mid);

    build(2*i+1,mid+1,r);

    tree[i] = max(tree[2*i],tree[2*i+1]);

}

ll query(int i,int l,int r,int ql,int qr)

{

    if(l > qr || r < ql)

        return 0;

    if(l >= ql && r <= qr)

        return tree[i];

    int mid = (l+r)/2; 

    ll ans1 = query(2*i,l,mid,ql,qr);

    ll ans2 = query(2*i+1,mid+1,r,ql,qr); 

    return max(ans1,ans2);

}

void update(int i,int l,int r,int j)

{

    if(l == r)

    {

        tree[i] = dp[l]; return;

    }

    int mid = (l+r)/2; 

    if(j <= mid)

        update(2*i,l,mid,j);

    else 

        update(2*i+1,mid+1,r,j);

    tree[i] = max(tree[2*i],tree[2*i+1]);

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    int n;

    cin>>n;

    int i;

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

        cin>>h[i]; 

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

        cin>>a[i]; 

    memset(dp,0,sizeof(dp)); 

    build(1,1,n); 

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

    {

        if(h[i] == 1)

            dp[h[i]] = a[i];

        else 

            dp[h[i]] = a[i] + query(1,1,MAXN,1,h[i]-1);

        update(1,1,MAXN,h[i]);

    }

    ll ans = 0;

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

    {

        ans = max(ans,dp[h[i]]);

    }

    cout<<ans<<"\n";

}

// Faster C++ solution for the same problem (measured speedup: 2.6557821366326295x):
#include <bits/stdc++.h>

#define LSOne(S) ((S) & (-S))

using namespace std;



typedef long long ll;



const int MAXN = 2*1e5 + 10;



ll bit[MAXN],dp[MAXN];

ll h[MAXN],a[MAXN];

int N;



void update(int x, ll val){

	while(x <= N){

		bit[x] = max(bit[x],val);

		x += LSOne(x);

	}

}



ll query(int x){



	ll ans = 0;

	while(x > 0){

		ans = max(ans,bit[x]);

		x -= LSOne(x);

	}



	return ans;



}



int main(){



	scanf("%d",&N);



	for(int i = 1;i<=N;i++){

		scanf("%lld",&h[i]);

	}

	for(int i = 1;i<=N;i++){

		scanf("%lld",&a[i]);

	}



	for(int i = 1;i<=N;i++){

		dp[i] = query(h[i] - 1) + a[i];

		update(h[i],dp[i]);

	}



	ll best = 0;

	for(int i = 1;i<=N;i++) best = max(best,dp[i]);

	printf("%lld\n",best);



	return 0;



}