#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()

using namespace std;

int n;
int s[3003];
int c[3003];

int main()
{
          //freopen("INPUT.txt","r",stdin);
          ios_base::sync_with_stdio(0);
          cin>>n;
          for(int i=0;i<n;i++)
                    cin>>s[i];
          for(int i=0;i<n;i++)
                    cin>>c[i];

          int ans=1000000000;
          for(int i=1;i<n-1;i++)
          {
                    int mn1=1000000000;
                    int mn2=1000000000;
                    for(int j=0;j<i;j++)
                              if(s[j]<s[i])
                                        mn1=min(mn1,c[j]);
                    for(int j=i+1;j<n-1;j++)
                              if(s[j]>s[i])
                                        mn2=min(mn2,c[j]);
                    ans=min(ans,mn1+mn2+c[i]);
          }
          if(ans==1000000000)
                    cout<<-1;
          else
                    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
int main()
{
    ll int n,i,j,k,res,d,s=0;
    cin>>n;
    ll int b[n],c[n];
    for(i=0;i<n;i++)
        cin>>b[i];
    for(i=0;i<n;i++){
        cin>>c[i];
        s=s+c[i];}
    for(i=0;i<n-2;i++){
        for(j=i+1;j<n-1;j++){
            for(k=j+1;k<n;k++){
                if(b[j]>b[i] && b[k]>b[j]){
                d=c[k]+c[j]+c[i];
                res=min(res,d);
                }
            }
        }
    }
    if(s<res)
        cout<<-1<<endl;
    else
    cout<<res<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
int main()
{
    ll int n,i,j,ans;
    cin>>n;
    ll int b[n],c[n],r[n];
    for(i=0;i<n;i++)
        cin>>b[i];
    for(i=0;i<n;i++)
        cin>>c[i];
    for(i=0;i<n;i++){
        r[i]=1e9;
        for(j=i+1;j<n;j++){
            if(b[j]>b[i])
                r[i]=min(r[i],c[j]);
        }
    }
    ans=1e9;
    for(i=0;i<n-1;i++){
        for(j=i+1;j<n;j++){
            if(b[j]>b[i])
                ans=min(ans,c[i]+c[j]+r[j]);
        }
    }
    if(ans>=1e9)
        cout<<-1<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
int main()
{
    int n,i,j,k,res,d,s=0;
    cin>>n;
    int b[n],c[n];
    for(i=0;i<n;i++)
        cin>>b[i];
    for(i=0;i<n;i++){
        cin>>c[i];
        s=s+c[i];}
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            for(k=j+1;k<n;k++){
                if(b[j]>b[i] && b[k]>b[j]){
                d=c[k]+c[j]+c[i];
                res=min(res,d);
                }
            }
        }
    }
    if(s<res)
        cout<<-1<<endl;
    else
    cout<<res<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int ans = 99999999, n, f[4][3002];
struct note
{
	int v;
	int t;
} a[3002];
int main()
{
	memset(f, 0x3f, sizeof(f));
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].t);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].v);
	for(int i = 1; i <= n; i++)
	f[1][i] = a[i].v;
	for(int i = 2; i <= 3; i++)
	   for(int j = n; j >= i; j--)
	      for(int k = 1; k < j; k++){
	      	if(a[k].t < a[j].t)
	         f[i][j] = min(f[i][j],f[i-1][k] + a[j].v);
	     }
	for(int i = 1; i <= n; i++)
	  ans=min(ans, f[3][i]);
    if(ans == 99999999) printf("-1");
	else printf("%d", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int ans = 99999999, n, f[4][3002];
struct note
{
	int v;
	int t;
} a[3002];
int main()
{
	memset(f, 0x3f, sizeof(f));
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].t);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].v);
	for(int i = 1; i <= n; i++)
	f[1][i] = a[i].v;
	for(int i = 2; i <= 3; i++)
	   for(int j = n; j >= i; j--)
	      for(int k = 1; k < j; k++){
	      	if(a[k].t < a[j].t)
	         f[i][j] = min(f[i][j],f[i-1][k] + a[j].v);
	     }
	for(int i = 1; i <= n; i++)
	  ans=min(ans, f[3][i]);
    if(ans == 99999999) printf("-1");
	else printf("%d", ans);
    retur


#include<bits/stdc++.h>
using namespace std;
int ans = 999999999, n, f[4][3002];
struct note
{
	int v;
	int t;
} a[3002];
int main()
{
	memset(f, 0x3f, sizeof(f));
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].t);
	for(int i = 1; i <= n; i++)
	   scanf("%d", &a[i].v);
	for(int i = 1; i <= n; i++)
	f[1][i] = a[i].v;
	for(int i = 2; i <= 3; i++)
	   for(int j = n; j >= i; j--)
	      for(int k = 1; k < j; k++){
	      	if(a[k].t < a[j].t)
	         f[i][j] = min(f[i][j],f[i-1][k] + a[j].v);
	     }
	for(int i = 1; i <= n; i++)
	  ans=min(ans, f[3][i]);
    if(ans == 999999999) printf("-1");
	else printf("%d", ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s1,s2,ans,a[10001],b[10001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s1=s2=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s1=min(s1,b[j]);
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s2=min(s2,b[j]);
		if ((s1==oo)||(s2==oo))break;
		ans=min(ans,s1+s2+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%lld",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
int n,s,sum,ans,a[3001],b[3001];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)scanf("%d",&b[i]);
	ans=0x3f3f3f3f;
	for(int i=2;i<n;i++){
		sum=b[i];
		s=0x3f3f3f3f;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s=min(s,b[j]);
		if (s==0x3f3f3f3f)break;
		sum+=s;
		s=0x3f3f3f3f;
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s=min(s,b[j]);
		if (s==0x3f3f3f3f)break;
		sum+=s;
		ans=min(ans,sum);
	}
	if (ans==0x3f3f3f3f)ans=-1;
	printf("%d",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s1,s2,ans,a[10001],b[10001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s1=s2=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s1=min(s1,b[j]);
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s2=min(s2,b[j]);
		if ((s1==oo)||(s2==oo))continue;
		ans=min(ans,s1+s2+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%lld",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s1,s2,ans,a[10001],b[10001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s1=s2=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s1=min(s1,b[j]);
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s2=min(s2,b[j]);
		printf("%d %lld %lld\n",i,s1,s2);
		if ((s1==oo)||(s2==oo))continue;
		ans=min(ans,s1+s2+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%lld",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s1,s2,ans,a[3001],b[3001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s1=s2=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s1=min(s1,b[j]);
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s2=min(s2,b[j]);
		if ((s1==oo)||(s2==oo))break;
		ans=min(ans,s1+s2+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%lld",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s,sum,ans,a[3001],b[3001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s=min(s,b[j]);
		if (s==oo)break;
		sum=s;
		s=oo;
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s=min(s,b[j]);
		if (s==oo)break;
		sum+=s;
		ans=min(ans,sum+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%lld",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define oo 0x3f3f3f3f
long long n,s,sum,ans,a[3001],b[3001];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ans=oo;
	for(int i=2;i<n;i++){
		s=oo;
		for(int j=1;j<i;j++)
			if (a[j]<a[i])s=min(s,b[j]);
		if (s==oo)break;
		sum=s;
		s=oo;
		for(int j=i+1;j<=n;j++)
			if (a[i]<a[j])s=min(s,b[j]);
		if (s==oo)break;
		sum+=s;
		ans=min(ans,sum+b[i]);
	}
	if (ans==oo)ans=-1;
	printf("%d",ans);
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;

 long long  c[3000][3000][4];

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
long long  a[3000];
int  b[3000];
 long long  c[3000][3][3000];

int f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;
        return c[i][t][pre]=0;

    } if (c[i][t][pre]!=-1)
    return c[i][t][pre];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t][pre]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
long long   b[3000];
 long long  c[3000][2999][4];

long long  f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    printf ("%lld",f(0,0,n)) ;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
int a[3000];
int  b[3000];
 long long  c[3000][3][3000];

int f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;
        return 0;

    } if (c[i][t][pre]!=-1)
    return c[i][t][pre];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t][pre]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

for (int i=0;i<n;i++)scanf ("%d",&a[i]);

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)scanf("%d",&b[i]);//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;

 long long  c[3000][3000][4];

long long  f(int i,int t,int pre,int a[],int b[]){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i,a,b);
 long long    c2=f(i+1,t,pre,a,b);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{int  a[3000];
int   b[3000];

    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n,a,b);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
int  b[3000];
 long long  c[3000][30][4];

int f(int i,int t,int pre){
     //if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
// long  c[3000][3000][4];
vector< vector < vector<long long > > > c;

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t])
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

//     memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
 long long c[3000][3000][4];
//vector< vector < vector<long long > > > c;

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t])
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

//     memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
long long  a[3000];
int  b[3000];
 long long  c[3000][3000][4];

int f(int i,int t,int pre){
     //if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
int a[3000];
int  b[3000];
 long long  c[3000][3][3000];

int f(int i,int t,int pre){
    if (i==n){

        if (t!=3)return c[i][t-1][pre]=1e9;
        return c[i][t-1][pre]=0;

    } if (c[i][t-1][pre]!=-1)
    return c[i][t-1][pre];
   if (t>3)return c[i][t-1][pre]=1e9;

if (t==3)return c[i][t-1][pre]=0;

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t-1][pre]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

for (int i=0;i<n;i++)scanf ("%d",&a[i]);

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)scanf("%d",&b[i]);//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;

 long long  c[3000][3000][4];

long long  f(int i,int t,int pre,int a[],int b[]){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i,a,b);
 long long    c2=f(i+1,t,pre,a,b);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{int  a[3000];
int   b[3000];

    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n,a,b)==1e9)cout<<-1;else
    printf ("%lld",f(0,0,n,a,b)) ;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
long long   b[3000];
 long long  c[3000][3000][3];

long long  f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    printf ("%lld",f(0,0,n)) ;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
 long c[3001][3001][4];
//vector< vector < vector<long long > > > c;

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

memset (c,-1,sizeof c);
    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
int a[3000];
int  b[3000];
 long long  c[3000][4][3000];

int f(int i,int t,int pre){
    if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;
        return c[i][t][pre]=0;

    } if (c[i][t][pre]!=-1)
    return c[i][t][pre];
   if (t>3)return c[i][t][pre]=1e9;

if (t==3)return c[i][t][pre]=0;

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t][pre]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

for (int i=0;i<n;i++)scanf ("%d",&a[i]);

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)scanf("%d",&b[i]);//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
 double c[3000][3000][4];
//vector< vector < vector<long long > > > c;

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

for (int i=0;i<3000;i++){
    for (int k=0;k<3000;k++)
    {for (int l=0;l<4;l++)
    c[i][k][l]=-1;

    }

}
    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n;
long long  a[3000];
int  b[3000];
 long long  c[3000][3][3000];

int f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;
        return 0;

    } if (c[i][t][pre]!=-1)
    return c[i][t][pre];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t][pre]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
 long  c[3000][3000][4];
//vector< vector < vector<long long > > > c;

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t])
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

//     memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
 long long c[3001][3][3001];
//vector< vector < vector<long long > > > c;

long long a[3001];
long long b [3001];

long long  f(int i,int t,int pre){
    if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][t][pre]!=-1)
    return c[i][t][pre];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][t][pre]= min(c1,c2);

}

int main()
{

memset (c,-1,sizeof c);
    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

 //long long x;
for (int i=0;i<n;i++)

    //scanf ("%d",&x);
  //  a.push_back(x);
//}

scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)
      //  long long x;
//scanf ("%d",&x);
        //b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

      //  >>b[i];
        scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

/*
for (int i=0;i<10;i++)
{
    for (int j=0;j<10;j++){
        for (int k=0;k<10;k++)cout<<c[i][j][k];

    }

}
*/
if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
long long   b[3000];
 long long  c[3000][3000][3];

int f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;

 int  c[3000][3000][4];

vector <int> a;
vector <int> b;

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][t][pre]=1e9;//c[i][pre][t]=1e9;
        return c[i][t][pre]=0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{

    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

    scanf ("%d",&x);
    a.push_back(x);
}

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++){
      //  long long x;
scanf ("%d",&x);
        b.push_back(x);}

        //cout<<a[0]<<a[1]<<b[0]<<b[1];

        //>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
long long j=f(0,0,n);

if (j==1e9)cout<<-1;
else cout<<j;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
long long   b[3000];
 long long  c[2999][2999][4];

long long  f(int i,int t,int pre){
  //   if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return 1e9;//c[i][pre][t]=1e9;
        return 0;//c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    printf ("%lld",f(0,0,n)) ;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
long long   b[3000];
 long long  c[3000][3000][3];

long long  f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int n;
long long  a[3000];
int  b[3000];
 long long  c[3000][3000][3];

int f(int i,int t,int pre){
     if (t>3)1e9;if (t==3) return 0;

  if (i==n){

        if (t!=3)return c[i][pre][t]=1e9;
        return c[i][pre][t]=0;

    } if (c[i][pre][t]!=-1)
    return c[i][pre][t];

    long long  c1=1e9
;    if (a[i]>a[pre])
    c1=b[i]+f(i+1,t+1,i);
 long long    c2=f(i+1,t,pre);
    return c[i][pre][t]= min(c1,c2);

}

int main()
{
    memset (c,-1,sizeof c);

    /*pair <long long ,long long > a[3010];
cin >>n;
for (int i=0;i<n;i++)
    scanf ("%lld",&a[i].first);
    for (int i=0;i<n;i++)
        scanf ("%d",&a[i].second);

    sort (a,a+n);

    long long b[3];
    for (int i=0;i<3;i++)
        b[i]=a[i].second;
    sort (b,b+3);

  //  cout<<b[0]<<b[1]<<b[2];

    for (int i=3;i<n;i++){
       if (a[i].second<b[0]){
            b[2]=b[1];
            b[1]=b[0];
            b[0]=a[i].second;}

     else   if (a[i].second<=b[1]){
            b[2]=b[1];
            b[1]=a[i].second;

        }

else if  (a[i].second<=b[2])
    b[2]=a[i].second;

        }

cout<<b[0]+b[1]+b[2];
    */

    scanf ("%d",&n);

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

//scanf ("%lld",&a[i]);
for (int i=0;i<n;i++)cin>>b[i];//scanf ("%lld",&b[i]);
a[n]=-1;
if (f(0,0,n)==1e9)cout<<-1;else
    cout << f(0,0,n) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3010][3010][4], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (!cur)
        return 0;

    if (arr[idx] <= arr[pre])
        return INT_MAX;

    if (cur && idx >= n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    return dp[pre][idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 3) >= (long long) (INT_MAX))
        cout << -1 << endl;
    else
        cout << dp[0][1][3] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3001][3001][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (!cur)
        return 0;

    if (cur && idx == n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    long long ret = INT_MAX;

    for (int i = idx; i < n; ++i)
    {
        if (arr[i] > arr[pre])
            ret = min(ret, brr[i] + solve(i, i + 1, cur - 1));
    }

    return dp[pre][idx][cur] = ret;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 3) >= INT_MAX)
        cout << -1 << endl;
    else
        cout << dp[0][1][3] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3010], brr[3010], dp[3010][3010][3];

int solve(int pre, int idx, int cur)
{
    if (cur == -1)
        return 0;

    if (cur >= 0 && idx >= n + 1)
        return 400000000;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    int ret = 400000000;

    if (arr[idx] <= arr[pre])
        ret = solve(pre, idx + 1, cur);
    else
        ret = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));

    return dp[pre][idx][cur] = ret;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 2) > 300000000)
        cout << -1 << endl;
    else
        cout << solve(0, 1, 2) << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3001][3001][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (!cur)
        return 0;

    if (cur && idx == n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    long long ret = INT_MAX;

    for (int i = idx; i < n; ++i)
    {
        if (arr[i] > arr[pre])
            ret = min(ret, brr[i] + solve(idx, i + 1, cur - 1));
    }

    return dp[pre][idx][cur] = ret;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 3) >= INT_MAX)
        cout << -1 << endl;
    else
        cout << dp[0][1][3] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3010], brr[3010], dp[3010][3010][3];

int solve(int pre, int idx, int cur)
{
    if (cur == -1)
        return 0;

    if (arr[idx] <= arr[pre])
        return 400000000;

    if (cur >= 0 && idx >= n + 1)
        return 400000000;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    return dp[pre][idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 2) > 300000000)
        cout << -1 << endl;
    else
        cout << solve(0, 1, 2) << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3001][3001][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (!cur)
        return 0;

    if (arr[idx] <= arr[pre])
        return INT_MAX;

    if (cur && idx == n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    return dp[pre][idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 3) >= INT_MAX)
        cout << -1 << endl;
    else
        cout << dp[0][1][3] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3010], brr[3010];

vector <int> vec[3010];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for (int i = 0; i < n; ++i)
        cin >> arr[i];

    for (int j = 0; j < n; ++j)
        cin >> brr[j];

    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (arr[i] < arr[j])
                vec[i].push_back(brr[j]);
        }
    }

    int ans = INT_MAX;

    for (int i = 0; i < n; ++i)
        sort(vec[i].begin(), vec[i].end());

    for (int i = 0; i < n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (arr[i] < arr[j] && vec[j].size())
                ans = min(ans, brr[i] + brr[j] + vec[j][0]);
        }
    }

    if (ans == INT_MAX)
        cout << -1 << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3010][3010][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (cur == -1)
        return 0;

    if (arr[idx] <= arr[pre])
        return INT_MAX;

    if (cur >= 0 && idx >= n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    return dp[pre][idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 2) >= (long long) (INT_MAX))
        cout << -1 << endl;
    else
        cout << dp[0][1][2] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3010][3010][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (!cur)
        return 0;

    if (arr[idx] <= arr[pre])
        return INT_MAX;

    if (cur && idx == n + 1)
        return INT_MAX;

    if (dp[pre][idx][cur] != -1)
        return dp[pre][idx][cur];

    return dp[pre][idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 3) >= INT_MAX)
        cout << -1 << endl;
    else
        cout << dp[0][1][3] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 101010;

int n, arr[3333];
long long dp[3010][3], brr[3333];

long long solve(int pre, int idx, int cur)
{
    if (cur == -1)
        return 0;

    if (arr[idx] <= arr[pre])
        return INT_MAX;

    if (cur >= 0 && idx >= n + 1)
        return INT_MAX;

    if (dp[idx][cur] != -1)
        return dp[idx][cur];

    return dp[idx][cur] = min(brr[idx] + solve(idx, idx + 1, cur - 1), solve(pre, idx + 1, cur));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    memset(dp, -1, sizeof(dp));

    cin >> n;

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

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

    if (solve(0, 1, 2) >= (long long) (INT_MAX))
        cout << -1 << endl;
    else
        cout << dp[1][2] << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7;

int n;
int s[N];
int c[N];

int dp[N][4]; // dp[i][j]: minimum cost so far on position (i) having (j) displays

main(){

    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1;

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 0 ; i <= n ; i ++) {
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = INF;
            }
            dp[i][0] = 0ll;
    }

    for(int i = 1 ; i <= n ; i ++){
            for(int j = 1 ; j <= 3 ; j ++){
                    for(int last = 0 ; last < i ; last ++){
                            if(s[last] < s[i]){
                                    dp[i][j] = min(dp[i][j] , c[i] + dp[last][j-1]);
                            }
                    }
            }
    }

    int ans = INF;
    for(int i = 1 ; i <= n ; i ++) ans = min(ans , dp[i][3]);

    if(ans == INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7;

int n;
int s[N];
int c[N];

int dp[N][4]; // dp[i][j]: minimum cost so far on position (i) having (j) displays

main(){

    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1;

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 0 ; i <= 3 ; i ++) dp[0][i] = INF;
    dp[0][0] = 0;

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

            // push dp values from last layer
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = dp[i-1][j];
            }

            // update dp values
            for(int last = 0 ; last < i ; last ++){

                    if(s[last] < s[i]){

                            for(int j = 1 ; j <= 3 ; j ++){
                                    dp[i][j] = min(dp[i][j] , c[i] + dp[last][j-1]);
                            }

                    }
            }
    }

    if(dp[n][3] == INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , dp[n][3]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7;

int n;
int s[N];
int c[N];

int dp[N][4]; // dp[i][j]: minimum cost so far on position (i) having (j) displays

main(){

    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1;

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 0 ; i <= n ; i ++) {
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = INF;
            }
            dp[i][0] = 0;
    }

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

            // push dp values from last layer
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = dp[i-1][j];
            }

            // update dp values
            for(int j = 1 ; j <= 3 ; j ++){
                    for(int last = 0 ; last < i ; last ++){

                            dp[i][j] = min(dp[i][j] , dp[last][j]);

                            if(s[last] < s[i]){

                                    dp[i][j] = min(dp[i][j] , c[i] + dp[last][j-1]);
                            }

                    }
            }
    }

    if(dp[n][3] == INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , dp[n][3]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N   = 3000 + 5;
const int INF = 2e9 + 7;

int n;
int s[N];
int c[N];

int st[5][N << 2];

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

    if(l == r){
            st[i][p] = INF;
            return;
    }

    int mid = (l + r) >> 1;

    build(i , p << 1     , l       , mid);
    build(i , p << 1 | 1 , mid + 1 ,   r);

    st[i][p] = min(st[i][p << 1] , st[i][p << 1 | 1]);
}

void update(int i , int p , int l , int r , int idx , int val){

    if(l == r){
            st[i][p] = min(st[i][p] , val);
            return;
    }

    int mid = (l + r) >> 1;

    if(idx <= mid) update(i , p << 1     , l       , mid , idx , val);
    else           update(i , p << 1 | 1 , mid + 1 , r   , idx , val);

    st[i][p] = min(st[i][p << 1] , st[i][p << 1 | 1]);
}

int rmq(int i , int p , int l , int r , int S , int E){
    if(l >= S && r <= E) return st[i][p];
    if(l > E || r < S) return INF;

    int mid = (l + r) >> 1;

    return min(rmq(i , p << 1 , l , mid , S , E) , rmq(i , p << 1 | 1 , mid + 1 , r , S , E));
}

main(){

    scanf("%d" , &n);

    map < int , int > compress;
    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
            compress[s[i]];
    }

    int cnt = 0;
    for(auto &C : compress){
            C.second = ++ cnt;
    }

    for(int i = 1 ; i <= n ; i ++){
            s[i] = compress[s[i]];
    }

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 1 ; i <= 3 ; i ++) build(i , 1 , 1 , cnt);

    for(int i = 1 ; i <= n ; i ++){
            for(int j = 1 ; j <= 3 ; j ++){
                    if(s[i] > 1) {
                            update(j , 1 , 1 , cnt , s[i] , c[i] + rmq(j-1 , 1 , 1 , cnt , 1 , s[i] - 1));
                    }
                    else if(j == 1){
                            update(j , 1 , 1 , cnt , s[i] , c[i]);
                    }
            }

    }

    printf("%d\n" , st[3][1] == INF ? -1 : st[3][1]);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair < int , int > ii;
const int N = 3003 , INF = 1e9;

int n;
ii a[N];

int mem[N][3][N];

int dp(int i , int take , int last)
{

    if(take == 3)
        return 0;

    if(i == n)
        return INF;

    if(last  !=  -1)
    if(mem[i][take][last] != -1)return mem[i][take][last];

    int ans = dp(i+1 , take,  last);

    if(a[i].first > a[last].first || last == -1)
        ans = min( a[i].second + dp(i+1 , take + 1 , i) , ans );

    return mem[i][take][last] = ans;
}

main()
{
    scanf("%d" , &n);

    for(int i=0;i<n;i++)
        scanf("%d" , &a[i].first);

    for(int i=0;i<n;i++)
        scanf("%d" , &a[i].second);

    memset(mem , -1 , sizeof mem);

    int  res = dp(0 , 0,  -1);

    if(res == INF)
        printf("-1\n");
    else printf("%d\n", res);

}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7; // largest value we can reach if (3 * 1e8), so (1e9 + 7) is large enough as INF

int n;
int s[N];
int c[N];

// minimum cost going from (this state having position and index of the last taken display
// and number of already take ones) to (the end)
int mem[N][N][4];

int dp(int i , int last , int cnt){
    if(cnt >= 3) return 0; // as soon as we reach 3 displays we return 0, we don't want cnt to get large
    if(i == n) return INF;

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

    // don't take this display
    int ans = dp(i + 1 , last , cnt);

    // try taking it if it is strictly bigger that the last taken one
    if(s[i] > s[last]){
            ans = min(ans , c[i] + dp(i + 1 , i , cnt + 1));
    }

    return mem[i][last][cnt] = ans;
}

main(){
    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1; // all the other ones are bigger

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    memset(mem , -1 , sizeof mem);

    int ans = dp(1 , 0 , 0); // last is 0, meaning we didn't take anything yet, value of s[0] = -1 so all other can be taken

    if(ans >= INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N   = 3000 + 5;
const int INF = 2e9 + 7;

int n;
int s[N];
int c[N];

int st[5][N << 2];

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

    if(l == r){
            st[i][p] = INF;
            return;
    }

    int mid = (l + r) >> 1;

    build(i , p << 1     , l       , mid);
    build(i , p << 1 | 1 , mid + 1 ,   r);

    st[i][p] = min(st[i][p << 1] , st[i][p << 1 | 1]);
}

void update(int i , int p , int l , int r , int idx , int val){

    if(l == r){
            st[i][p] = min(st[i][p] , val);
            return;
    }

    int mid = (l + r) >> 1;

    if(idx <= mid) update(i , p << 1     , l       , mid , idx , val);
    else           update(i , p << 1 | 1 , mid + 1 , r   , idx , val);

    st[i][p] = min(st[i][p << 1] , st[i][p << 1 | 1]);
}

int rmq(int i , int p , int l , int r , int S , int E){
    if(l >= S && r <= E) return st[i][p];
    if(l > E || r < S) return INF;

    int mid = (l + r) >> 1;

    return min(rmq(i , p << 1 , l , mid , S , E) , rmq(i , p << 1 | 1 , mid + 1 , r , S , E));
}

main(){

    scanf("%d" , &n);

    map < int , int > compress;
    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
            compress[s[i]];
    }

    int cnt = 1;
    for(auto &C : compress){
            C.second = ++ cnt;
    }

    for(int i = 1 ; i <= n ; i ++){
            s[i] = compress[s[i]];
    }

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 1 ; i <= 3 ; i ++) build(i , 1 , 1 , cnt);

    for(int i = 1 ; i <= n ; i ++){
            for(int j = 1 ; j <= 3 ; j ++){
                    update(j , 1 , 1 , cnt , s[i] , c[i] + rmq(j-1 , 1 , 1 , cnt , 1 , s[i] - 1));
            }

    }

    printf("%d\n" , st[3][1] == INF ? -1 : st[3][1]);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7;

int n;
int s[N];
int c[N];

int dp[N][4]; // dp[i][j]: minimum cost so far on position (i) having (j) displays

main(){

    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1;

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 0 ; i <= n ; i ++) {
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = INF;
            }
            dp[i][0] = 0ll;
    }

    for(int i = 1 ; i <= n ; i ++){
            for(int j = 1 ; j <= 3 ; j ++){
                    for(int last = 0 ; last < i ; last ++){
                            if(s[last] < s[i]){
                                    dp[i][j] = min(dp[i][j] , c[i] + dp[last][j-1]);
                            }
                    }
            }

            for(int j = 1 ; j <= 3 ; j ++){
                    dp[i][j] = min(dp[i][j] , dp[i-1][j]);
            }
    }

    int ans = dp[n][3];
    //for(int i = 1 ; i <= n ; i ++) ans = min(ans , dp[i][3]);

    if(ans == INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef pair < int , int > ii;
const int N = 3003 , INF = 5e8;

int n;
ii a[N];

int mem[N][3][N];

int dp(int i , int take , int last)
{

    if(take == 3)
        return 0;

    if(i == n)
        return INF;

    if(last != -1)
    if(mem[i][take][last] != -1)return mem[i][take][last];

    int ans = INF;

    if(last == -1)
        ans = min( min(a[i].second + dp(i+1 , take + 1 , i) , dp(i+1 , take , last)) , ans );

    if(a[i].first > a[last].first)
        ans = min( min(a[i].second + dp(i+1 , take + 1 , i) , dp(i+1 , take , last)) , ans );

    return mem[i][take][last] = ans;
}

main()
{
    memset(mem , -1 , sizeof mem);
    scanf("%d" , &n);

    for(int i=0;i<n;i++)
        scanf("%d" , &a[i].first);

    for(int i=0;i<n;i++)
        scanf("%d" , &a[i].second);

    int  res = dp(0 , 0,  -1);

    if(res == INF)
        printf("-1\n");
    else printf("%d\n", res);

}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 3000 + 5;
const ll INF = 1e12 + 7;

int n;
int s[N];
int c[N];

ll dp[N][4]; // dp[i][j]: minimum cost so far on position (i) having (j) displays

main(){

    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1;

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    for(int i = 0 ; i <= n ; i ++) {
            for(int j = 0 ; j <= 3 ; j ++){
                    dp[i][j] = INF;
            }
            dp[i][0] = 0ll;
    }

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

            // update dp values
            for(int j = 1 ; j <= 3 ; j ++){
                    for(int last = 0 ; last < i ; last ++){
                            if(s[last] < s[i]){
                                    dp[i][j] = min(dp[i][j] , 1ll*c[i] + dp[last][j-1]);
                            }
                    }
            }
            ans = min(ans , dp[i][3]);
    }

    if(ans == INF){
            printf("-1\n");
    }
    else {
            printf("%lld\n" , ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair < ll , ll > ii;
const ll N = 3030 , INF = 1e18;

ll n;
ii a[N];

ll mem[N][N][6];

ll dp(ll i , ll take , ll last)
{

    if(take == 3)
        return 0;

    if(i == n)
        return INF;

    if(last != -1)
    if(mem[i][take][last] != -1)return mem[i][take][last];

    ll ans = INF;

    if(last == -1)
        ans = min( min(a[i].second + dp(i+1 , take + 1 , i) , dp(i+1 , take , last)) , ans );

    if(a[i].first > a[last].first)
        ans = min( min(a[i].second + dp(i+1 , take + 1 , i) , dp(i+1 , take , last)) , ans );

    return mem[i][take][last] = ans;
}

main()
{
    memset(mem , -1 , sizeof mem);
    scanf("%lld" , &n);

    for(int i=0;i<n;i++)
        scanf("%lld" , &a[i].first);

    for(int i=0;i<n;i++)
        scanf("%lld" , &a[i].second);

    ll  res = dp(0 , 0,  -1);

    if(res == INF)
        printf("-1\n");
    else printf("%lld\n", res);

}


#include <bits/stdc++.h>

using namespace std;

const int N = 3000 + 5;
const int INF = 1e9 + 7; // largest value we can reach if (3 * 1e8), so (1e9 + 7) is large enough as INF

int n;
int s[N];
int c[N];

// minimum cost going from (this state having position and index of the last taken display
// and number of already take ones) to (the end)
int mem[N][N][4];

int dp(int i , int last , int cnt){
    if(cnt >= 3) return 0; // as soon as we reach 3 displays we return 0, we don't want cnt to get large
    if(i == n+1) return INF;

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

    // don't take this display
    int ans = dp(i + 1 , last , cnt);

    // try taking it if it is strictly bigger that the last taken one
    if(s[i] > s[last]){
            ans = min(ans , c[i] + dp(i + 1 , i , cnt + 1));
    }

    return mem[i][last][cnt] = ans;
}

main(){
    scanf("%d" , &n);

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &s[i]);
    }
    s[0] = -1; // all the other ones are bigger

    for(int i = 1 ; i <= n ; i ++){
            scanf("%d" , &c[i]);
    }

    memset(mem , -1 , sizeof mem);

    int ans = dp(1 , 0 , 0); // last is 0, meaning we didn't take anything yet, value of s[0] = -1 so all other can be taken

    if(ans >= INF){
            printf("-1\n");
    }
    else {
            printf("%d\n" , ans);
    }

    return 0;
}


/*#          #  # # # # #  # # # # #  # # # # #  #          #
  #          #  #       #  #       #  #          #          #
  # # # # # ##  # # # # #  ## # # #   # # # # #  # # # # # ##
  #          #  #       #  #    #             #  #          #
  #          #  #       #  #      #   # # # # #  #          #*/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define debug1(a) cout<<a<<endl;
#define debug2(a,b) cout<<a<<' '<<b<<endl;
#define debug3(a,b,c) cout<<a<' '<<b<<' '<<c<<endl;
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,a,b)for(int i=a;i<b;i++)
#define repre(i,a,b)for(int i=a;i<=b;i++)
#define pi pair<int,int>
#define pii pair<int,pi>
#define mp map<int,int>
#define ump unordered_map<int,int>
#define st set<int>
#define ust unordered_set<int>
#define mst multiset<int>
#define pq priority_queue
#define mpq priority_queue<int,vector<int>,grater<int> >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define sumofdigits(sum,num) int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; }
#define int2str(str,n) n=boost::lexical_cast<int>(str);
#define float2str(str,n) n=boost::lexical_cast<float>(str);
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> orderedSet;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> orderedMSet;
//*p.find_by_order(index) return value at index
//p.order_of_key(key) return index
int32_t main()
{
    fastio
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n,f=1;
        cin>>n;
        int a[n],b[n];
        rep(i,n)cin>>a[i];
        rep(i,n)cin>>b[i];
        int ans=INT_MAX;
        int i=0,j=1,k=2;
        while(i<n-2)
        {
          while(j<n-1)
          {
            while(k<n)
            {
              if(a[i]<a[j] && a[j]<a[k])
              {
                //cout<<a[i]*b[i]+a[j]*b[j]+a[k]*b[k]<<endl;
                f=0;
                ans=min(ans,b[i]+b[j]+b[k]);
              }
              k++;
              if(k==n-1)goto w;
            }
            j++;
            if(j==n-2)goto w;
          }
          i++;
          if(i==n-3)goto w;
        }
        w:if(!f)cout<<ans<<endl;
        else cout<<"-1";
    }
    return 0;
}


import java.io.*; import java.util.*;
public class CF0449D {

    static int MXBIT=20;
    static int MAXN=1<<20;
    static int MOD=1000000007;
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

    }
    public static void main(String[] args) {
        int[] pow2=new int[MAXN+1];
        pow2[0]=1;
        for (int i = 1; i <=MAXN; i++) {
            pow2[i]=(pow2[i-1]*2)%MOD;
        }
        FastReader br=new FastReader();
        int N=br.nextInt();
        int[] a=new int[N];
        int[] dp=new int[MAXN];
        for (int i = 0; i < N; i++) {
            a[i]=br.nextInt();
            dp[a[i]]++;//Evaluates f(mask,0)
        }
        for (int i = 0; i < MXBIT; i++) {
            for (int mask = 0; mask < MAXN; mask++) {//Memory compressed:
                //After processing i, everything is updated
                if((mask&(1<<i))==0){
                    dp[mask]+=dp[mask|(1<<i)];
                }
            }
        }
        int ans=0; boolean bit=false;
        for (int mask = 0; mask < MAXN; mask++) {
            bit=false;
            for (int i = 0; i < N; i++) {
                if((mask&(1<<i))>0){
                    bit=!bit;
                }
            }
            if(bit){
                ans-=pow2[dp[mask]];
            }else{
                ans+=pow2[dp[mask]];
            }
            ans%=MOD;
        }
        if(ans<0){
            ans+=MOD;
        }

        System.out.println(ans);
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Rubanenko
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    final int MAX_VALUE = 1 << 20;
    final int MD = 1000000000 + 7;
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = new int[MAX_VALUE];
        int[] c = new int[MAX_VALUE];
        int[] deg = new int[MAX_VALUE];
        long[] f = new long[MAX_VALUE];
        deg[0] = 1;
        for (int i = 1; i < MAX_VALUE; i++) {
            deg[i] = deg[i - 1] * 2;
            if (deg[i] >= MD) deg[i] -= MD;
        }
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
           // int x = i;
            a[x]++;
            f[x]++;
            c[x]++;
        }
        long[] ff = new long[MAX_VALUE];
        int ans = deg[n] - 1;
        if (ans < 0) ans += MD;
        long[] fact = new long[23];
        fact[0] = 1;
        for (int i = 1; i <= 21; i++) {
            fact[i] = fact[i - 1] * ((long) i);
        }

        for (int j = 0; j < 20; j++) {
        for (int mask = MAX_VALUE - 1; mask > 0; mask--) {
                if (f[mask] > 0) {
                    for (int i = 0; i < 20; i++) {
                        if (((1 << i) & mask) > 0) ff[mask ^ (1 << i)] += f[mask];
                    }
                }
            }
            for (int mask = MAX_VALUE - 1; mask > 0; mask--) {
                c[mask] += ff[mask] / fact[j];
                f[mask] = ff[mask];
                ff[mask] = 0;
            }
        }
        for (int mask = 1; mask < MAX_VALUE; mask++) {
            if ((Integer.bitCount(mask) & 1) == 1) ans -= deg[c[mask]] - 1;
            else ans += deg[c[mask]] - 1;
            if (ans >= MD) ans -= MD;
            if (ans < 0) ans += MD;
        }
        out.println(ans);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public String nextLine() {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return line;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(nextLine());
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Rubanenko
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    final int MAX_VALUE = 1 << 20;
    final int MD = 1000000000 + 7;
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = new int[MAX_VALUE];
        int[] c = new int[MAX_VALUE];
        int[] deg = new int[MAX_VALUE];
        long[][] f = new long[20][MAX_VALUE];
        deg[0] = 1;
        for (int i = 1; i < MAX_VALUE; i++) {
            deg[i] = deg[i - 1] * 2;
            if (deg[i] >= MD) deg[i] -= MD;
        }
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
           // int x = i;
            a[x]++;
            f[0][x]++;
        }
        int ans = deg[n] - 1;
        if (ans < 0) ans += MD;
        for (int mask = MAX_VALUE - 1; mask > 0; mask--) {
            for (int j = 0; j < 20; j++) {
                if (f[j][mask] > 0) {
                    for (int i = 0; i < 20; i++) {
                        if (((1 << i) & mask) > 0) f[j + 1][mask ^ (1 << i)] += f[j][mask];
                    }
                }
            }
        }
        long[] fact = new long[23];
        fact[0] = 1;
        for (int i = 1; i <= 21; i++) {
            fact[i] = fact[i - 1] * ((long) i);
        }
        for (int mask = 0; mask < MAX_VALUE; mask++) {
            for (int j = 0; j < 20; j++) {
                c[mask] += f[j][mask] / fact[j];
            }
        }
        for (int mask = 1; mask < MAX_VALUE; mask++) {
            if ((Integer.bitCount(mask) & 1) == 1) ans -= deg[c[mask]] - 1;
            else ans += deg[c[mask]] - 1;
            if (ans >= MD) ans -= MD;
            if (ans < 0) ans += MD;
        }
        out.println(ans);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public String nextLine() {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return line;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(nextLine());
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Rubanenko
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    final int MAX_VALUE = 1 << 20;
    final int MD = 1000000000 + 7;
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] a = new int[MAX_VALUE];
        int[] c = new int[MAX_VALUE];
        int[] deg = new int[MAX_VALUE];
        long[] f = new long[MAX_VALUE];
        deg[0] = 1;
        for (int i = 1; i < MAX_VALUE; i++) {
            deg[i] = deg[i - 1] * 2;
            if (deg[i] >= MD) deg[i] -= MD;
        }
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
           // int x = i;
            a[x]++;
            f[x]++;
            c[x]++;
        }
        long[] ff = new long[MAX_VALUE];
        int ans = deg[n] - 1;
        if (ans < 0) ans += MD;
        long[] fact = new long[23];
        fact[0] = 1;
        for (int i = 1; i <= 21; i++) {
            fact[i] = fact[i - 1] * ((long) i);
        }

        for (int j = 0; j < 20; j++) {
        for (int mask = MAX_VALUE - 1; mask > 0; mask--) {
                if (f[mask] > 0) {
                    for (int i = 0; i < 20; i++) {
                        if (((1 << i) & mask) > 0) ff[mask ^ (1 << i)] += f[mask];
                    }
                }
            }
            for (int mask = MAX_VALUE - 1; mask > 0; mask--) {
                c[mask] += ff[mask] / fact[j + 1];
                f[mask] = ff[mask];
                ff[mask] = 0;
            }
        }
        for (int mask = 1; mask < MAX_VALUE; mask++) {
            if ((Integer.bitCount(mask) & 1) == 1) ans -= deg[c[mask]] - 1;
            else ans += deg[c[mask]] - 1;
            if (ans >= MD) ans -= MD;
            if (ans < 0) ans += MD;
        }
        out.println(ans);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public String nextLine() {
        String line = null;
        try {
            line = reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return line;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens())
            tokenizer = new StringTokenizer(nextLine());
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class MainClass
{
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        int[] A = new int[n];   for (int i=0;i<n;i++)   A[i] = in.nextInt();
        int[] dp = new int[1 << 20];    int univMask = (1 << 20) - 1;
        for (int i=0;i<n;i++) ++dp[univMask ^ A[i]];
        for (int i=0;i<20;i++)
            for (int mask=0;mask<(1 << 20);++ mask)
                if ((mask & (1 << i)) > 0)
                    dp[mask] += dp[mask ^ (1 << i)];
        int[] count = new int[1 << 20];
        for (int mask=0;mask<(1 << 20);mask++)   count[mask] += dp[mask ^ univMask];
        long M = 1_000_000_007L;
        long ans = 0L;
        for (int mask = 0;mask < (1 << 20); ++ mask)
        {
            long pow = binPow(count[mask], M);
            int x = Integer.bitCount(mask) % 2; if (x == 1) x = -1; else x = 1;
            ans = (ans + (1L * x * pow) % M + M) % M;
        }
        System.out.println(ans);
    }
    public static long binPow(int n, long M)
    {
        if (n == 0) return 1;
        long res = binPow(n / 2, M);
        res = (res * res) % M;
        if (n % 2 == 1) res = (res * 2L) % M;
        return res;
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class MainClass
{
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        int[] A = new int[n];   for (int i=0;i<n;i++)   A[i] = in.nextInt();
        int[] dp = new int[1 << 20];    int univMask = (1 << 20) - 1;
        for (int i=0;i<n;i++) ++dp[univMask ^ A[i]];
        for (int i=0;i<20;i++)
            for (int mask=0;mask<(1 << 20);++ mask)
                if ((mask & (1 << i)) > 0)
                    dp[mask] += dp[mask ^ (1 << i)];
        int[] count = new int[1 << 20];
        for (int mask=0;mask<(1 << 20);mask++)   count[mask] += dp[mask ^ univMask];
        long M = 1_000_000_007;
        long ans = 0L;
        for (int mask = 0;mask < (1 << 20); ++ mask)
        {
            long pow = binPow(count[mask], (int)M);
            int x = Integer.bitCount(mask) % 2; if (x == 1) x = -1; else x = 1;
            ans = (ans + (1L * x * pow) % M) % M;
        }
        System.out.println(ans);
    }
    public static long binPow(int n, int M)
    {
        if (n == 0) return 1;
        long res = binPow(n / 2, M);
        res = (res * res) % M;
        if (n % 2 == 1) res = (res * 2L) % M;
        return res;
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class MainClass
{
    public static void main(String[] args)throws IOException
    {
        Reader in = new Reader();
        int n = in.nextInt();
        int[] A = new int[n];   for (int i=0;i<n;i++)   A[i] = in.nextInt();
        int[] dp = new int[1 << 20];    int univMask = (1 << 20) - 1;
        for (int i=0;i<n;i++) ++dp[univMask ^ A[i]];
        for (int i=0;i<20;i++)
            for (int mask=0;mask<(1 << 20);++ mask)
                if ((mask & (1 << i)) > 0)
                    dp[mask] += dp[mask ^ (1 << i)];
        int[] count = new int[1 << 20];
        for (int mask=0;mask<(1 << 20);mask++)   count[mask] += dp[mask ^ univMask];
        long M = 1_000_000_007;
        long ans = 0L;
        for (int mask = 0;mask < (1 << 20); ++ mask)
        {
            long pow = binPow(count[mask], M);
            int x = Integer.bitCount(mask) % 2; if (x == 1) x = -1; else x = 1;
            ans = (ans + (1L * x * pow) % M) % M;
        }
        System.out.println(ans);
    }
    public static long binPow(int n, long M)
    {
        if (n == 0) return 1;
        long res = binPow(n / 2, M);
        res = (res * res) % M;
        if (n % 2 == 1) res = (res * 2L) % M;
        return res;
    }
}
class Reader
{
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader()
    {
        din = new DataInputStream(System.in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public Reader(String file_name) throws IOException
    {
        din = new DataInputStream(new FileInputStream(file_name));
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String readLine() throws IOException
    {
        byte[] buf = new byte[64]; // line length
        int cnt = 0, c;
        while ((c = read()) != -1)
        {
            if (c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        return new String(buf, 0, cnt);
    }

    public int nextInt() throws IOException
    {
        int ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do
        {
            ret = ret * 10 + c - '0';
        }  while ((c = read()) >= '0' && c <= '9');

        if (neg)
            return -ret;
        return ret;
    }

    public long nextLong() throws IOException
    {
        long ret = 0;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();
        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');
        if (neg)
            return -ret;
        return ret;
    }

    public double nextDouble() throws IOException
    {
        double ret = 0, div = 1;
        byte c = read();
        while (c <= ' ')
            c = read();
        boolean neg = (c == '-');
        if (neg)
            c = read();

        do {
            ret = ret * 10 + c - '0';
        }
        while ((c = read()) >= '0' && c <= '9');

        if (c == '.')
        {
            while ((c = read()) >= '0' && c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if (neg)
            return -ret;
        return ret;
    }

    private void fillBuffer() throws IOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1)
            buffer[0] = -1;
    }

    private byte read() throws IOException
    {
        if (bufferPointer == bytesRead)
            fillBuffer();
        return buffer[bufferPointer++];
    }

    public void close() throws IOException
    {
        if (din == null)
            return;
        din.close();
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class D_Round_257_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int[]data = new int[n];
        long[]count = new long[1<<20];
        for(int i = 0; i < n; i++){
        	data[i] = in.nextInt();
        	count[data[i]]++;
        }
      //  System.out.println("HE HE");

        for(int i = 0; i < 20; i++){
        	for(int j = (1 << 20) - 1; j >= 0; j-- ){
        		if(((1<<i) & j) == 0){
        			count[j] += count[j | (1<<i)];
        		}
        	}
        }
        long result = 0;
        for(int i = 0; i < 1<<20; i++){
        	long sign = Integer.bitCount(i) % 2 == 0 ? 1 : -1;
        	result += sign * pow(2, count[i]);
        	result %= MOD;
        }
        out.println(result);
        out.close();
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return x - o.x;
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}