stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<cstdio>  

#include<iostream>  

#include<algorithm>  

#include<cstdlib>  

#include<cstring>

#include<string>

#include<climits>

#include<vector>

#include<cmath>

#define LL long long



using namespace std;



inline char nc(){

  static char buf[100000],*p1=buf,*p2=buf;

  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }

  return *p1++;

}



inline void read(int &x){

  char c=nc(),b=1;

  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;

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

}



inline void read(LL &x){

  char c=nc(),b=1;

  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;

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

}



int wt,ss[19];

inline void print(int x){

	if (x<0) x=-x,putchar('-'); 

	if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}

}

inline void print(LL x){

	if (x<0) x=-x,putchar('-');

	if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}

}



LL n,m;



int main()

{

	cin>>n;

	m=n/11;

	if (n-11*m==0) cout<<2*m<<endl;

	else if (n-11*m>6) cout<<2*m+2<<endl;

	else cout<<2*m+1<<endl;

 

 return 0;

 

}

// Optimized solution to the same problem (faster):
#include<cstdio>

typedef long long ll;

int main(){

  ll x,ans;

  scanf("%lld",&x);

  ans=x/11*2;

  if (x%11!=0)

    if (x%11>6)

      ans+=2;

    else

      ans++;

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

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <iostream>

#include <algorithm>

typedef long long ll;

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

const ll inf = 1000000000000000007LL;

using namespace std;

ll w[10][10], ans[10][10], tmp[10][10], m;

bool solve(ll n) {

	rep(i, 1, 6) rep(j, 1, 6) w[i][j] = - inf;

	rep(i, 1, 6) rep(j, 1, 6) if (i != j && i + j != 7) w[j][i] = i;

	bool flg = true;

	for ( ; n; n /= 2) {

		if (n & 1LL) {

			if (flg) {

				memcpy(ans, w, sizeof(w));

				flg = false;

			} else {

				rep(i, 1, 6) rep(j, 1, 6) tmp[i][j] = - inf;

				rep(i, 1, 6) rep(j, 1, 6) rep(k, 1, 6) 

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

				memcpy(ans, tmp, sizeof(tmp));

			}

		}

		rep(i, 1, 6) rep(j, 1, 6) tmp[i][j] = - inf;

		rep(i, 1, 6) rep(j, 1, 6) rep(k, 1, 6)

			tmp[i][j] = max(tmp[i][j], w[i][k] + w[k][j]);

		memcpy(w, tmp, sizeof(tmp));

	}

	ll mx = - inf;

	rep(i, 1, 6) rep(j, 1, 6) mx = max(mx, ans[i][j]);

	return (mx >= m);

}

int main() {

	scanf("%lld", &m);

	ll l = 1, r = inf, ans;

	for ( ; l <= r; ) {

		ll mid = (l + r) / 2;

		if (solve(mid)) ans = mid, r = mid - 1;

			else l = mid + 1;

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include <set>

#include <map>

#include <queue>

#include <ctime>

#include <cmath>

#include <cstdio>

#include <vector>

#include <string>

#include <cctype>

#include <bitset>

#include <cstring>

#include <cstdlib>

#include <utility>

#include <iostream>

#include <algorithm>

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

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

#define RVC(i,S) for(int i=0;i<(S).size();i++)

#define RAL(i,u) for(int i=fr[u];i!=-1;i=e[i].next)

using namespace std;

typedef long long LL;

typedef pair<int,int> pii;

     

template<class T> inline

void read(T& num) {

    bool start=false,neg=false;

    char c;

    num=0;

    while((c=getchar())!=EOF) {

        if(c=='-') start=neg=true;

        else if(c>='0' && c<='9') {

            start=true;

            num=num*10+c-'0';

        } else if(start) break;

    }

    if(neg) num=-num;

}

/*============ Header Template ============*/

 

int main() {

    LL n;

    read(n);

    LL m=2*(n/11);n%=11;

    if(n) {

        if(n<=6) m+=1; else m+=2;

    }

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

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



typedef long long int64;



int64 memo[1111][1111];



 



int64 dfs(int i,int cnt);



string t,b;



 



int main(){



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



    for(int j = 0;j < 1111;j++){



      memo[i][j] = -1;



    }



  }



  t += '0';



  string a;



  cin >> a >> b;



  t += a;



  cout << dfs(0,0) << endl;



  return 0;



}



 



int64 dfs(int i,int cnt){



  int rec = 0;



  if(cnt == b.size())return 1;



  if(memo[i][cnt] != -1)return memo[i][cnt];



  for(int j = i + 1;j < t.size();j++){



    if(t[j] == b[cnt])rec = (rec + dfs(j,cnt + 1)) % 1000000007;



  }



  return memo[i][cnt] = rec % 1000000007;



}

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

using namespace std;

 

#define int long long 

 

main()

{

  string s, r;

  int dp[1005][1005] = {};

  int mod = 1000000007;

   

  dp[0][0] = 1;

   

  cin >> s >> r;

   

  for(int i = 0; i < s.size(); i++) {

    for(int j = 0; j <= r.size(); j++) {

      (dp[i + 1][j] += dp[i][j]) %= mod;

      if(j != r.size() && s[i] == r[j]) (dp[i + 1][j + 1] += dp[i][j]) %= mod;

    }

  }

 

  cout << dp[s.size()][r.size()] << endl;

 

  return (0);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

   

  

  

  

}


// Optimized solution to the same problem (faster):
#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;

	

	

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

}

// Optimized solution to the same problem (faster):
#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;



}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



using ll = long long;



const int N = 2e5 + 5;

 

int n;

long long f[N];

pair <int, int> a[N];

 

struct FenwickTree {

    long long bit[N];

    void init(){

        memset(bit, 0, sizeof bit);

    }

    

    void update (int x, long long val) {

        for (int i = x; i < N; i += i & -i) bit[i] = max(bit[i], val);

    }

    

    long long query (int x) {

        long long ans = 0;

        for (int i = x; i > 0; i -= i & -i) ans = max(ans, bit[i]);

        return ans;

    }

} fw;

 

int main()

{

   

    cin >> n; fw.init();

    for (int i = 1; i <= n; i++) cin >> a[i].first;

    for (int i = 1; i <= n; i++) cin >> a[i].second;

    for (int i = 1; i <= n; i++) f[i] = a[i].second;

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

        f[i] = max(f[i], fw.query(a[i].first - 1) + a[i].second);

        fw.update(a[i].first, f[i]);

    }

    

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

    return 0;

}

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

using namespace std;

using ll = long long;



const int MAX_N = 2e5+10;

int N;

int h[MAX_N];

int a[MAX_N];

ll V[MAX_N+1];

ll dp[MAX_N+1];



void update(int i, ll x)

{

	while(i<=MAX_N){

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

		i+=(i&-i);

	}

}



ll query(int i)

{

	ll res = 0;

	while(i>0){

		res =max(res, V[i]);

		i -= (i&-i);

	}

	return res;

}



void solve()

{

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

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

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

	}

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

}



int main()

{

	cin >> N;

	for(int i=1;i<=N;++i) cin >> h[i];

	for(int i=1;i<=N;++i) cin >> a[i];

	solve();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

 

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

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

#define FRSZ(i, a) for(int i = 0 ; i < a.size() ; ++i)

#define FDSZ(i, a) for(int i = a.size() - 1 ; i >= 0 ; --i)

 

#define debug(x) cout << #x << " = " << x << endl;

#define debugvi(x) {FRSZ(_, x) cout << x[_] << " "; cout << endl;}

#define debugarr(x, n) {FOR(_, 1, n) cout << x[_] << " "; cout << endl;}

 

#define fi first

#define se second

#define pb push_back

using namespace std;

 

typedef int64_t ll;

const int MAX = 2e5 + 5;

int n;

int a[MAX], h[MAX];

 

struct FenwickTree{

    ll Tree[MAX];

    void Update(int u, ll x){

        for(; u <= n ; u += u & -u )

            Tree[u] = max(Tree[u], x);

    }

 

    ll Query(int u){

        if (u <= 0) return 0;

        ll ans = 0;

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

            ans = max(ans, Tree[u]);

        return ans;

    }

} BIT;

 

int main(){



 

    cin >> n;

    FOR(i, 1, n) cin >> h[i];

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

 

    ll ans = 0;    

    memset(BIT.Tree, 0, sizeof(BIT.Tree));

    FOR(i, 1, n){

        ll tmp = BIT.Query(h[i] - 1) + a[i];

        ans = max(ans, tmp);

        BIT.Update(h[i], tmp);

    }

    cout << ans;

    return 0;

}

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

#define ll long long

#define pb push_back

#define pf push_front

#define popb pop_back

#define popf pop_front

#define in insert

#define b back

#define f front

#define p push

#define FOR(i,a,b) for (ll i=a; i<=b; i++)

#define FOD(i,a,b) for (ll i=a; i>=b; i--)

#define mp make_pair

#define up upper_bound

#define lp lower_bound

#define endl "\n"

//priority_queue <int> ; less

//priority_queue <int,vector<int>,greater<int> > ; greater

using namespace std;

ll n,i,a[200005],kq[200005];

set <ll> dp;

set <ll> ::iterator it,tg;

int main()

{

	ios_base::sync_with_stdio(0);

	cin.tie(NULL);

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	cin>>n;

	for (i=1;i<=n;i++) cin>>a[i];

	for (i=1;i<=n;i++) cin>>kq[a[i]];

	dp.in(a[1]);

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

	{

		it=dp.up(a[i]);

		if (it==dp.end())

		{

			tg=dp.end();

			tg--;

			kq[a[i]]+=kq[*tg];

			dp.in(a[i]);

		}

			else 

			{

				if (it==dp.begin())

				{

					while(kq[*it]<=kq[a[i]]&&it!=dp.end()) 

					{

						dp.erase(it);

						it=dp.up(a[i]);

					}

					dp.in(a[i]);

				}

				else

				{

					tg=it;

					tg--;

					kq[a[i]]+=kq[*tg];

					while(kq[*it]<=kq[a[i]]&&it!=dp.end()) 

					{

						dp.erase(it);

						it=dp.up(a[i]);

					}

					dp.in(a[i]);

				}

			}

	}

	cout<<kq[*(--dp.end())];

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

const int N = 2e5;

vector<ll> tree(4 * N, 0);



void update(int l, int r, int idx, ll val, int node)

{

	if (l == r)

	{

		tree[node] = val;

	}

	else

	{

		int m = l + (r - l) / 2;

		if (idx >= l && idx <= m)

		{

			update(l, m, idx, val, 2 * node);

		}

		else

		{

			update(m + 1, r, idx, val, 2 * node + 1);

		}

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

	}

}



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

{

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

	{

		return tree[node];

	}

	if (ql > r || qr < l)

	{

		return 0;

	}

	int m = l + (r - l) / 2;

	return max(query(l, m, ql, qr, 2 * node), query(m + 1, r, ql, qr, 2 * node + 1));

}



int main()

{

	ios_base::sync_with_stdio(false);



	int n;

	cin >> n;

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

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

	{

		cin >> h[i];

	}

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

	{

		cin >> a[i];

	}

	ll answer = 0;

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

	{

		ll temp = query(1, n, 1, h[i] - 1, 1) + a[i];

		answer = max(answer, temp);

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

	}

	cout << answer;



	return 0;

}

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

using namespace std;

typedef long long ll;



const int mxN = 2e5 + 5;

int N, h[mxN], a[mxN];

ll tree[4 * mxN];



ll query(int s, int e, int ql, int qr, int node) {

	if (ql <= s && qr >= e) {

		return tree[node];

	}

	if (qr < s || ql > e) {

		return 0;

	}

	int mid = s + (e - s) / 2;

	return max(query(s, mid, ql, qr, 2 * node), query(mid + 1, e, ql, qr, 2 * node + 1));

}



void update(int l, int r, int idx, ll val, int node) {

	if (l == r) {

		tree[node] = val;

	} else {

		int mid = l + (r - l) / 2;

		if (idx >= l && idx <= mid) {

			update(l, mid, idx, val, 2 * node);

		} else if (idx >= mid + 1 && idx <= r) {

			update(mid + 1, r, idx, val, 2 * node + 1);

		}

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

	}

}



int main() {

	ios_base::sync_with_stdio(false);



	cin >> N;

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

		cin >> h[i];

	}

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

		cin >> a[i];

	}

	ll answer = 0;

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

		ll val = a[i] + query(1, N, 1, h[i] - 1, 1);

		answer = max(answer, val);

		update(1, N, h[i], val, 1);

	}

	cout << answer;



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

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

using namespace std;

typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }



#define def 0

template<class V, int NV> struct SegTree { //[l,r)

    V comp(V& l, V& r) { return max(l, r); };



    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }

    V get(int x, int y, int l = 0, int r = NV, int k = 1) {

        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];

        auto a = get(x, y, l, (l + r) / 2, k * 2); 

        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);

        return comp(a, b);

    }

    void update(int i, V v) {

        i += NV; val[i] = v;

        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);

    }

    void add(int i, V v) { update(i, val[i + NV] + v); }

    V operator[](int x) { return get(x, x + 1); }

};

int N, H[200010], A[200010];

SegTree<ll, 1 << 18> dp;



int main(){

    cin >> N;

    rep(i, 0, N) cin >> H[i];

    rep(i, 0, N) cin >> A[i];



    rep(i, 0, N) {

        ll opt = dp.get(0, H[i]) + A[i];

        dp.update(H[i], max(dp[H[i]], opt));

    }



    cout << dp.get(0, 1<<18) << endl;

}

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

using namespace std;

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

typedef long long ll;



const int MAX_N = 1 << 18;

const int def = 0;



//セグメント木を持つグローバル配列

ll n, dat[2 * MAX_N - 1];



//初期化

void init(ll n_){

    //簡単のため、要素数を2のべき乗に

    n = 1;

    while ( n < n_ ) n *= 2;



    //全ての値をINT_MAXに

    for(int i = 0; i < 2 * n - 1; i++) dat[i] = def;

}



//k番目の値(0-indexed)をaに変更

void update(ll k, ll a){

    //葉の節点

    k += n - 1;

    dat[k] = a;

    //登りながら更新

    while(k > 0){

        k = (k-1)/2;

        dat[k] = max(dat[k*2+1], dat[k*2+2]);

    }

}



// [a,b)の最小値を求める

// 後ろの方の引数は、計算の簡単のための引数

// kは節点の番号、1,rはその接点が[1,r)に対応づいていることを表す

// したがって、外からはquery(a, b, 0, 0, n)として呼ぶ

ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = n){

    //[a,b)と[l,r)が交差しなければ、INT_MAX

    if( r <= a || b <= l) return def;



    //[a,b)が[l,r)を完全に含んでいれば、この節点の値

    if(a <= l && r <= b) return dat[k];

    else{

        //そうでなければ、２つの子が必要

        ll vl = query(a, b, k * 2 + 1, l, (l+r)/2);

        ll vr = query(a, b, k * 2 + 2, (l+r)/2, r);

        return max(vl,vr);

    }

}



int main(){

    ll N; cin >> N;

    vector<ll> h(N), a(N);

    rep(i, 0, N) cin >> h[i];

    rep(i, 0, N) cin >> a[i];



    init(N);

    rep(i, 0, N){

        update(h[i]-1, query(0, h[i]-1) + a[i]);

    }

    cout << query(0, N) << endl;

}

// Fastest known solution for this problem: