#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    for(int k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    reverse(p,p+n);
    c1=pow(10,16);
    lli c2=c1;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    for(int k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c1>s)
        c1=s;
    }
    }
    c=min(c,c1);
    if(c==c2)
    cout<<-1;
    else
    cout<<c;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    if(max==h)
    continue;
    int k;
    int p4;
    for(k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    for(k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    if((max1==h))
    {continue;}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<int,int> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli c=INT_MAX;
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
        int b,b1;
        int max=INT_MAX;
        int max1=INT_MAX;
        int p4=max;
        int flag=1;
        for(int j=i+1;j<=n;j++)
        {
         if(p[j-1].first>p[i-1].first)
         {
             if(max>p[j-1].second)
             {
                 max=p[j-1].second;
                 b=j;
             }
         }
        }
        for(int k=i+1;k<=n;k++)
        {
        if(k<b)
        {
        if((p[k-1].first>p[i-1].first)&&(p[k-1].first<p[b-1].first))
         {
             if(max1>p[k-1].second)
             {
                 max1=p[k-1].second;
                 b1=k;
             }
         }
        }
        else if(k>b)
        {
        if((p[k-1].first>p[i-1].first)&&(p[k-1].first>p[b-1].first))
         {
             if(max1>p[k-1].second)
             {
                 max1=p[k-1].second;
                 b1=k;
             }
         }
        }
        }
        if((p4==max1)||(p4==max))
        flag=0;
        lli s=INT_MAX;
        if(flag)
        s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<int,int> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli c=INT_MAX;
    lli c1=c;
    for(int i=1;i<=n-2;i++)
    {
        int b,b1;
        int max=INT_MAX;
        int max1=INT_MAX;
        int p4=max;
        int flag=1;
        for(int j=i+1;j<=n;j++)
        {
         if(p[j-1].first>p[i-1].first)
         {
             if(max>p[j-1].second)
             {
                 max=p[j-1].second;
                 b=j;
             }
         }
        }
        for(int k=i+1;k<=n;k++)
        {
        if(k<b)
        {
        if((p[k-1].first>p[i-1].first)&&(p[k-1].first<p[b-1].first))
         {
             if(max1>p[k-1].second)
             {
                 max1=p[k-1].second;
                 b1=k;
             }
         }
        }
        else if(k>b)
        {
        if((p[k-1].first>p[i-1].first)&&(p[k-1].first>p[b-1].first))
         {
             if(max1>p[k-1].second)
             {
                 max1=p[k-1].second;
                 b1=k;
             }
         }
        }
        }
        if((p4==max1)||(p4==max))
        flag=0;
        lli s=INT_MAX;
        if(flag)
        s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<int,int> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    int b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    int max,max1;max=max1=INT_MAX;
    int h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    for(int k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    if(max==h)
    continue;
    int k;
    int p4;
    for(k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    for(k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    for(k=1;k<i;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first<p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    if((max1==h))
    {continue;}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    int k;
    int p4;
    for(k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    for(k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            {max1=p[k-1].second;
            p4=k;}
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<int,int> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    int b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    int max,max1;max=max1=INT_MAX;
    int h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=i+1;k<=n;k++)
    {
    if((p[k-1].first>p[i-1].first)&&(p[k-1].first!=p[b-1].first))
    {
        if((p[k-1].first>p[b-1].first)&&(k>b))
        {
            if(max1>p[k-1].second)
        {
            max1=p[k-1].second;
        }
        }
        else if((p[k-1].first<p[b-1].first)&&(k<b))
        {
            if(max1>p[k-1].second)
        {
            max1=p[k-1].second;
        }
        }
    }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int j=0;j<i;j++)
    {
        if(p[j].first<p[i-1].first)
    {
        if(max1>p[j].second)
        {
            max1=p[j].second;
        }
    }
    }
    if((max1==h)||(max==h))
    {continue;}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli max=1e18;
    int b;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(p[j-1].first>p[i-1].first)
            {
                if(max>(p[j-1].second+p[i-1].second))
                max=(p[j-1].second+p[i-1].second),b=j;
            }
        }
    }
    lli max1=1e18;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if((p[j-1].first>p[i-1].first)&&(p[j-1].first>p[b-1].first)&&(j>b))
            {
               if(max1>(max+p[j-1].second))
               max1=max+p[j-1].second;
            }
            else if((p[j-1].first>p[i-1].first)&&(p[j-1].first<p[b-1].first)&&(j<b))
            {
                if(max1>(max+p[j-1].second))
               max1=max+p[j-1].second;
            }
        }
    }
    if(max1==1e18)
    cout<<-1;
    else
    cout<<max1;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=pow(10,16);
    lli c1=c;
    for(int i=1;i<=n;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    for(int k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mem(a) memset(a,0,sizeof(a))
int a4[100007];
long long int c1=0;
vector<int> v2;
lli p;
int flag=0;
#define make_pair mp
typedef pair<lli,lli> ll;
bool cmp(ll p1,ll p2)
{
    if(p1.first==p2.first)
    return p1.second<p2.second;
    return p1.first>p2.first;
}
main()
{
    lli n,k;
    cin>>n;
    ll p[n];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].first;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>p[i-1].second;
    }
    lli b;
    lli c=1e18;
    lli c1=c;
    for(int i=1;i<=n-2;i++)
    {
    lli max,max1;max=max1=INT_MAX;
    lli h=max;
    for(int j=i+1;j<=n;j++)
    {
    if(p[j-1].first>p[i-1].first)
    {
        if(max>p[j-1].second)
        {
            max=p[j-1].second;
            b=j;
        }
    }
    }
    for(int k=b+1;k<=n;k++)
    {
        if((p[k-1].first>p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    for(int k=i+1;k<b;k++)
    {
        if((p[k-1].first<p[b-1].first)&&(p[k-1].first>p[i-1].first))
        {
            if(max1>p[k-1].second)
            max1=p[k-1].second;
        }
    }
    if((max==h)||(max1==h))
    {}
    else
    {
        lli s=max+max1+p[i-1].second;
        if(c>s)
        c=s;
    }
    }
    if(c==c1)
    cout<<-1;
    else
    cout<<c;

}


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

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 2000000000;//0x3f3f3f3f;
const ll mod = 1000000007;

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

    int n;
    cin >> n;
    vi s(n), c(n);
    for(int i = 0; i < n; i++) cin >> s[i];
    for(int i = 0; i < n; i++) cin >> c[i];
    int ans = inf;
    for(int i = 1; i < n-1; i++){
        int f = inf;
        for(int j = 0; j < i; j++) if(s[j]<s[i]) f = min(f, c[j]);
        int l = inf;
        for(int j = i+1; j < n; j++) if(s[j]>s[i]) l = min(l, c[j]);
        ans = min(ans, c[i]+f+l);
    }
    if(ans == inf) cout << "-1\n";
    else cout << ans << "\n";
}


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

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<unsigned int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const unsigned int inf = 2000000000;//0x3f3f3f3f;
const ll mod = 1000000007;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    register unsigned int i,j;
    int n;
    cin >> n;
    vi s(n), c(n);
    for( i = 0; i < n; i++) cin >> s[i];
    for( i = 0; i < n; i++) cin >> c[i];
    unsigned int ans = inf;
    for( i = 1; i < n-1; i++){
        unsigned int f = inf;
        for( j = 0; j < i; j++) if(s[j]<s[i]) f = min(f, c[j]);
        unsigned int l = inf;
        for(j = i+1; j < n; j++) if(s[j]>s[i]) l = min(l, c[j]);
        ans = min(ans, c[i]+f+l);
    }
    if(ans == inf) cout << "-1\n";
    else cout << ans << "\n";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    register unsigned int i,j;
    unsigned long a,b,c;
    int n;
    std::vector<unsigned long> v1;
    vector<unsigned long> v2;
    cin>>n;
    v1.assign(n,0);
    v2.assign(n,0);
    for(i=0;i<n;i++)
    {
        cin>>v1[i];
    }
    for(i=0;i<n;i++)
    {
        cin>>v2[i];
    }
    a=b=c=-1;
    c/=2;
    for(i=1;i<n-1;++i)
    {
        a=-1;
        a/=2;
        for(j=0;j<i;++j)
        {
            if(v1[i]>v1[j])
            {
                a=min(a,v2[j]);
            }
        }
        b=-1;
        b/=2;
        for(j=i+1;j<n;++j)
        {
            if(v1[i]<v1[j])
            {
                b=min(b,v2[j]);
            }
        }
        //cout<<c<<" "<<a<<" "<<b<<" "<<v2[i]<<"\n";
        c=min(c,a+b+v2[i]);
        //cout<<c<<"\n";
    }
    if(c==9223372036854775807)
        cout<<"-1";
    else
        cout<<c;
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    register unsigned int i,j;
    unsigned long a,b,c;
    int n;
    std::vector<unsigned long> v1;
    vector<unsigned long> v2;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a;
        v1.push_back(a);
    }
    for(i=0;i<n;i++)
    {
        cin>>a;
        v2.push_back(a);
    }
    c=2000000000;
    for(i=1;i<n-1;++i)
    {
        a=2000000000;
        for(j=0;j<i;++j)
        {
            if(v1[i]>v1[j])
            {
                a=min(a,v2[j]);
            }
        }
        b=2000000000;
        for(j=i+1;j<n;++j)
        {
            if(v1[i]<v1[j])
            {
                b=min(b,v2[j]);
            }
        }
        //cout<<c<<" "<<a<<" "<<b<<" "<<v2[i]<<"\n";
        c=min(c,a+b+v2[i]);
        //cout<<c<<"\n";
    }
    if(c==2000000000)
        cout<<"-1";
    else
        cout<<c;
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    register unsigned int i,j;
    unsigned long a,b,c;
    int n;
    std::vector<unsigned long> v1(n);
    vector<unsigned long> v2(n);
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>v1[i];
    }
    for(i=0;i<n;i++)
    {
        cin>>v2[i];
    }
    c=2000000000;
    for(i=1;i<n-1;++i)
    {
        a=2000000000;
        for(j=0;j<i;++j)
        {
            if(v1[i]>v1[j])
            {
                a=min(a,v2[j]);
            }
        }
        b=2000000000;
        for(j=i+1;j<n;++j)
        {
            if(v1[i]<v1[j])
            {
                b=min(b,v2[j]);
            }
        }
        //cout<<c<<" "<<a<<" "<<b<<" "<<v2[i]<<"\n";
        c=min(c,a+b+v2[i]);
        //cout<<c<<"\n";
    }
    if(c==2000000000)
        cout<<"-1";
    else
        cout<<c;
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    register unsigned int i,j;
    unsigned long a,b,c;
    int n;
    std::vector<unsigned long> v1;
    vector<unsigned long> v2;
    cin>>n;
    v1.assign(n,0);
    v2.assign(n,0);
    for(i=0;i<n;i++)
    {
        cin>>v1[i];
    }
    for(i=0;i<n;i++)
    {
        cin>>v2[i];
    }
    c=2000000000;
    for(i=1;i<n-1;++i)
    {
        a=2000000000;
        for(j=0;j<i;++j)
        {
            if(v1[i]>v1[j])
            {
                a=min(a,v2[j]);
            }
        }
        b=2000000000;
        for(j=i+1;j<n;++j)
        {
            if(v1[i]<v1[j])
            {
                b=min(b,v2[j]);
            }
        }
        //cout<<c<<" "<<a<<" "<<b<<" "<<v2[i]<<"\n";
        c=min(c,a+b+v2[i]);
        //cout<<c<<"\n";
    }
    if(c==2000000000)
        cout<<"-1";
    else
        cout<<c;
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    register unsigned int i,j;
    unsigned long a,b,c;
    int n;
    std::vector<unsigned long> v1;
    vector<unsigned long> v2;
    cin>>n;
    v1.assign(n,0);
    v2.assign(n,0);
    for(i=0;i<n;i++)
    {
        cin>>v1[i];
    }
    for(i=0;i<n;i++)
    {
        cin>>v2[i];
    }
    a=b=c=-1;
    c/=2;
    for(i=1;i<n-1;++i)
    {
        a=-1;
        a/=2;
        for(j=0;j<i;++j)
        {
            if(v1[i]>v1[j])
            {
                a=min(a,v2[j]);
            }
        }
        b=-1;
        b/=2;
        for(j=i+1;j<n;++j)
        {
            if(v1[i]<v1[j])
            {
                b=min(b,v2[j]);
            }
        }
        //cout<<c<<" "<<a<<" "<<b<<" "<<v2[i]<<"\n";
        c=min(c,a+b+v2[i]);
        //cout<<c<<"\n";
    }
    a=-1;
    a/=2;
    if(c==a)
        cout<<"-1";
    else
        cout<<c;
    return(0);
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
int dp[3005][3005][5];
int arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n){
        if(c==0)return 0;
        else return INT_MAX;
    }
    if(c==0)return 0;
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=INT_MAX;
    if(arr[i]>arr[prev] || c==3)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,i,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long  ans=solve(0,0,3);
    if(ans==INT_MAX)cout<<-1;
    else cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
long long dp[3005][3005][5];
int arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n){
        if(c==0)return 0;
        else return INT_MAX;
    }
    if(c==0)return 0;
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=INT_MAX;
    if(arr[i]>arr[prev] || c==0)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,i,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long  ans=solve(0,0,3);
    if(ans==INT_MAX)cout<<-1;
    else cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
int dp[3005][3005][5];
int arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n){
        if(c==0)return 0;
        else return INT_MAX;
    }
    if(c==0)return 0;
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=INT_MAX;
    if(arr[i]>arr[prev] || i==0)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,i,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long  ans=solve(0,0,3);
    if(ans==INT_MAX)cout<<-1;
    else cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
int dp[3005][3005][5];
long long arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n ||c==0){
        if(c==0)return 0;
        else return 1000000009;
    }
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=1000000009;
    if(arr[i]>arr[prev] || c==3)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,prev,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long ans=solve(0,0,3);
    if(ans==1000000009)cout<<-1;
    else cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
int dp[3005][3005][5];
int arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n){
        if(c==0)return 0;
        else return INT_MAX;
    }
    if(c==0)return 0;
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=INT_MAX;
    if(arr[i]>arr[prev] || c==0)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,i,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long  ans=solve(0,0,3);
    if(ans==INT_MAX)cout<<-1;
    else cout<<ans;
return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int root(int arr[],int i){
    while(arr[i]!=i){
         arr[i]=arr[arr[i]];
        i=arr[i];
    }
    return i;
}
void Union(int arr[],int Size[],int a,int b){
int rootA=root(arr,a);
int rootB=root(arr,b);
if(Size[rootA]<Size[rootB]){
    arr[rootA]=arr[rootB];
    Size[rootB]+=Size[rootA];
}else{
arr[rootB]=arr[rootA];
Size[rootA]+=Size[rootB];
}
}
bool findRoot(int a,int b,int arr[]){
 if(root(arr,a)==root(arr,b))return true;
 else return false;
}
 vector<vector<int> >adj(105);
bool nCyc=false;
/*void dijkstra(int v,long long dis[]){
dis[v]=0;
multiset<pair<int,int> >s;
s.insert({0,v});
while(!s.empty()){
    pair<int,int>p= *s.begin();
    s.erase(s.begin());
    int x=p.second;
  if(vis[x])continue;
    vis[x]=1;
    for(int i=0;i<adj[x].size();i++){
        int e=adj[x][i].first,w=adj[x][i].second;
      //  cout<<dis[e]<<" "<<dis[x]+w<<endl;
        if(dis[e]>dis[x]+w){
            dis[e]=dis[x]+w;
            s.insert(make_pair(dis[e],e));
        }
    }
}
}*/
/*void bfs(int s){
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int v=q.front();
        q.pop();
        for(int i=0;i<adj[v].size();i++){
            if(!vis[adj[v][i]]){
                 q.push(adj[v][i]);
                 vis[adj[v][i]]=1;
            }
        }
    }
}*/
int vis[55][55];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
/*bool isVal(int i,int j){
      if(i<0 || j<0 || j>=m || i>=n)return false;
      return true;
}*/
/*void dfs(int i,int j) {

    if(!isVal(i,j) || vis[i][j] || arr[i][j]=='#')return;
      vis[i][j]=1;
	  if(arr[i][j]=='G'){
	  check++;
	  }
	  if(arr[i][j]=='B'){
        b=1;
        return;
	  }
	for(int k=0;k<4;k++){
     int pos1=i+dx[k],pos2=j+dy[k];
   //  cout<<pos1<<" "<<pos2<<endl;
     if( isVal(pos1,pos2) && (arr[pos1][pos2]=='#'))continue;
               dfs(pos1,pos2);
	}
}
void dfs2(int i,int j){
 if(isVal(i,j)==false || arr[i][j]!='.')return;
  arr[i][j]='#';
}*/
/*
double prim(int x){
    priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;
    double min_cost=0.0;
    pair<double,int>p;
     q.push(make_pair(0.0,x));
     while(!q.empty()){
      p=q.top();
      q.pop();
      x=p.second;
      if(vis[x])continue;
      min_cost+=p.first;
      vis[x]=1;
      for(int i=0;i<adj[x].size();i++){
         int y=adj[x][i].second;
         if(!vis[y]){
            q.push(adj[x][i]);
        }
      }
     }
     return min_cost;
}*/
int dp[3005][3005][5];
long long arr[3005],arr2[3005];
int n;
long long solve(int i,int prev,int c){
    if(i==n ||c==0){
        if(c==0)return 0;
        else return 1000000009;
    }
    if(dp[i][prev][c]!=-1)return dp[i][prev][c];
    long long ret=1000000009;
    if(arr[i]>arr[prev] || c==3)ret=min(ret,solve(i+1,i,c-1)+arr2[i]);
    ret=min(ret,solve(i+1,prev,c));
   return dp[i][prev][c]=ret;
}
int freq[200005],check[100005];
int main()
{

    cin>>n;
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++)cin>>arr2[i];
    memset(dp,-1,sizeof dp);
    long long ans=solve(0,0,3);
    if(ans==INT_MAX)cout<<-1;
    else cout<<ans;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long

int main()
{
	ios_base::sync_with_stdio(false);
	ll n;
	cin>>n;
	vector<ll> s(n+1),c(n+1);
	vector< pair<ll,  pair<ll,ll> >  >v;
	f(i,0,n) cin>>s[i];
	f(i,0,n) cin>>c[i];
	f(i,0,n)
	{
		v.pb(mp(c[i],mp(s[i],i)));
	}
	sort(v.begin(),v.end());
	//v.insert(v.end(),v.begin(),v.end());
	vector<ll> ans;
	//ll a[3];
	f(i,0,n)
	{
		ll csn=v[i].second.second,cs=v[i].second.first,cc=v[i].first,ctc=0;
		ll cn=csn;
		f(j,csn+1,n)
		{
			if(v[j].second.first>cs and v[j].second.second>cn )
			{
				ctc++;
				cc+=v[j].first;
				//cs=v[j].second.first;
				//cn=v[j].second.second;
				//cc=v[j].first

			}
			else if(v[j].second.first<cs and v[j].second.second<cn )
			{
				ctc++;
				cc+=v[j].first;
			}
			if(ctc==2)
			{
				ans.pb(cc);
				break;
			}
			//if(cc<2 and j==n-1)

		}
		if(ctc==2)
			ans.pb(cc);

	}
	sort(ans.begin(),ans.end());
	if(ans.empty())
		cout<<-1<<endl;
	else
		cout<<ans[0]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(ll i=x; i<y ;i++)

int main()
{
	ios_base::sync_with_stdio(false);
	ll n;
	cin>>n;
	vector<ll> s(n+1),c(n+1);
	vector< pair<ll,  pair<ll,ll> >  >v;
	f(i,0,n) cin>>s[i];
	f(i,0,n) cin>>c[i];
	f(i,0,n)
	{
		v.pb(mp(c[i],mp(s[i],i)));
	}
	sort(v.begin(),v.end());
	//v.insert(v.end(),v.begin(),v.end());
	vector<ll> ans;
	//ll a[3];
	f(i,0,n)
	{
		ll csn=v[i].second.second,cs=v[i].second.first,cc=v[i].first,ctc=0;
		ll cn=csn;
		f(j,csn+1,n)
		{
			if(v[j].second.first>cs and v[j].second.second>cn )
			{
				ctc++;
				cc+=v[j].first;
				//cs=v[j].second.first;
				//cn=v[j].second.second;
				//cc=v[j].first

			}
			else if(v[j].second.first<cs and v[j].second.second<cn )
			{
				ctc++;
				cc+=v[j].first;
			}
			if(ctc==2)
			{
				ans.pb(cc);
				break;
			}
			//if(cc<2 and j==n-1)

		}
		if(ctc==2)
			ans.pb(cc);

	}
	sort(ans.begin(),ans.end());
	if(ans.empty())
		cout<<-1<<endl;
	else
		cout<<ans[0]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long
#define F first
#define S second

int main()
{
	std::ios_base::sync_with_stdio(false);

	int n;
	cin>>n;
	ll ar[n],cst[n];
	f(i,0,n) cin>>ar[i];
	f(i,0,n) cin>>cst[i];
	int i=1;
	vector<int> ls[n];
	ls[0].pb(cst[0]);
	int dp[n];
	dp[0]=1;
	while(i<n)
	{
		vector<int> v;
		dp[i]=1;
		f(j,0,i)
		{
			if(ar[j]< ar[i] and dp[j]>= dp[i])
			{
				dp[i]=1+dp[j];
				v=ls[j];
			}
		}
		ls[i]=v;
		ls[i].pb(cst[i]);
		i++;
	}
	ll ans= INT_MAX;
	int flg=0;
	f(i,2,n)
	{
		if(ls[i].size()>=3)
		{
			flg++;
			sort(ls[i].begin(),ls[i].end());
			if(ans> ls[i][0]+ls[i][1]+ls[i][2])
			{
				ans> ls[i][0]+ls[i][1]+ls[i][2];
			}
		}
	}
	if(flg)
	cout<<ans<<endl;
	else cout<<-1<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long
#define F first
#define S second

int main()
{
	std::ios_base::sync_with_stdio(false);

	int n;
	cin>>n;
	ll ar[n],cst[n];
	f(i,0,n) cin>>ar[i];
	f(i,0,n) cin>>cst[i];
	int i=1;
	vector<int> ls[n];
	ls[0].pb(cst[0]);
	int dp[n];
	dp[0]=1;
	while(i<n)
	{
		vector<int> v;
		dp[i]=1;
		f(j,0,i)
		{
			if(ar[j]< ar[i] and dp[j]>= dp[i])
			{
				dp[i]=1+dp[j];
				v=ls[j];
			}
		}
		ls[i]=v;
		ls[i].pb(cst[i]);
		i++;
	}
	int ans= INT_MAX;
	int flg=0;
	f(i,2,n)
	{
		if(ls[i].size()>=3)
		{
			flg++;
			sort(ls[i].begin(),ls[i].end());
			ans= min(ans, ls[i][0]+ls[i][1]+ls[i][2]);
		}
	}
	if(flg)
	cout<<ans<<endl;
	else cout<<-1<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> s(n+1),c(n+1);
	vector< pair<int,  pair<int,int> >  >v;
	f(i,0,n) cin>>s[i];
	f(i,0,n) cin>>c[i];
	f(i,0,n)
	{
		v.pb(mp(c[i],mp(s[i],i)));
	}
	sort(v.begin(),v.end());
	//v.insert(v.end(),v.begin(),v.end());
	vector<int> ans;
	//int a[3];
	f(i,0,n)
	{
		int csn=v[i].second.second,cs=v[i].second.first,cc=v[i].first,ctc=0;
		int cn=csn;
		f(j,csn+1,n)
		{
			if(v[j].second.first>cs and v[j].second.second>cn )
			{
				ctc++;
				cc+=v[j].first;
				//cs=v[j].second.first;
				//cn=v[j].second.second;
				//cc=v[j].first

			}
			else if(v[j].second.first<cs and v[j].second.second<cn )
			{
				ctc++;
				cc+=v[j].first;
			}
			if(ctc==2)
			{
				ans.pb(cc);
				break;
			}
			//if(cc<2 and j==n-1)

		}
		if(ctc==2)
			ans.pb(cc);

	}
	sort(ans.begin(),ans.end());
	if(ans.empty())
		cout<<-1<<endl;
	else
		cout<<ans[0]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long

int main()
{
	ios_base::sync_with_stdio(false);
	ll n;
	cin>>n;
	vector<ll> s(n+1),c(n+1);
	vector< pair<ll,  pair<ll,ll> >  >v;
	f(i,0,n) cin>>s[i];
	f(i,0,n) cin>>c[i];
	f(i,0,n)
	{
		v.pb(mp(c[i],mp(s[i],i)));
	}
	sort(v.begin(),v.end());
	//v.insert(v.end(),v.begin(),v.end());
	vector<ll> ans;
	//ll a[3];
	f(i,0,n)
	{
		ll csn=v[i].second.second,cs=v[i].second.first,cc=v[i].first,ctc=0;
		ll cn=csn;
		f(j,i+1,n)
		{
			if(v[j].second.first>cs and v[j].second.second>cn )
			{
				ctc++;
				cc+=v[j].first;
				//cs=v[j].second.first;
				//cn=v[j].second.second;
				//cc=v[j].first

			}
			else if(v[j].second.first<cs and v[j].second.second<cn )
			{
				ctc++;
				cc+=v[j].first;
			}
			if(ctc==2)
			{
				ans.pb(cc);
				break;
			}
			//if(cc<2 and j==n-1)

		}
		if(ctc==2)
			ans.pb(cc);

	}
	sort(ans.begin(),ans.end());
	if(ans.empty())
		cout<<-1<<endl;
	else
		cout<<ans[0]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long
#define F first
#define S second

int main()
{
	std::ios_base::sync_with_stdio(false);

	int n;
	cin>>n;
	ll ar[n],cst[n];
	f(i,0,n) cin>>ar[i];
	f(i,0,n) cin>>cst[i];
	int i=1;
	vector<int> ls[n];
	ls[0].pb(cst[0]);
	int dp[n];
	dp[0]=1;
	while(i<n)
	{
		vector<int> v;
		dp[i]=1;
		f(j,0,i)
		{
			if(ar[j]< ar[i] and dp[j]>= dp[i])
			{
				dp[i]=1+dp[j];
				v=ls[j];
			}
		}
		ls[i]=v;
		ls[i].pb(cst[i]);
		i++;
	}
	ll ans= INT_MAX;
	int flg=0;
	f(i,2,n)
	{
		if(ls[i].size()>=3)
		{
			flg++;
			sort(ls[i].begin(),ls[i].end());
			if(ans> ls[i][0]+ls[i][1]+ls[i][2])
			{
				ans= ls[i][0]+ls[i][1]+ls[i][2];
			}
		}
	}
	if(flg)
	cout<<ans<<endl;
	else cout<<-1<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define  pb(x) push_back(x)
#define  mp(x,y) make_pair(x,y)
#define f(i,x,y) for(int i=x; i<y ;i++)
#define ll long long
#define F first
#define S second

int main()
{
	std::ios_base::sync_with_stdio(false);

	int n;
	cin>>n;
	ll ar[n],cst[n];
	f(i,0,n) cin>>ar[i];
	f(i,0,n) cin>>cst[i];
	int i=1;
	vector<int> ls[n];
	ls[0].pb(cst[0]);
	int dp[n];
	dp[0]=-1;
	if(ar[1]>ar[0]) dp[1]=0;
	i=2;
	ll ans= INT_MAX;int flg=0;
	while(i<n)
	{
		dp[i]=-1;
		int cv=INT_MAX;
		f(j,0,i)
		{
			if(ar[j]<ar[i])
			{
				if(dp[j]!=-1)
				{
					flg++;
					if(cst[i]+cst[j]+cst[dp[j]]<ans)
						ans= cst[j]+cst[i]+cst[dp[j]];
				}
				if(cst[j]<cv)
				{
					cv=cst[j];
					dp[i]=j;
				}
			}
		}
		i++;
	}

	if(flg)
	cout<<ans<<endl;
	else cout<<-1<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={};
    long long g[n]={};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
                if(g[i]==0)g[i]=c[i]+c[j];
                else if(c[i]+c[j]<g[i])g[i]=c[i]+c[j];
            }
        }
    }
    long long ans=3000000001;
    long long che=ans;
    for(int i=0;i<n-2;i++){
/*        for(int j=0;j<n-1;j++){
            if(a[i][j]==0)break;
            int x=a[i][j];
            if(g[x]!=0)if(ans>c[i]+g[x])ans=c[i]+g[x];
            for(int k=0;k<n-1;k++){
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans){ans=c[i]+c[a[i][j]]+c[a[x][k]];}
            }
        }*/
        for(int j=i+1;j<n-1;j++){
            if(a[i][j]==0)break;
            if(c[i]+g[j]<ans && g[j]!=0)ans=c[i]+g[j];
        }
    }
    if(ans==che)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
            }
        }
    }

    long long ans=3000000001;
    long long che=ans;
    for(int i=0;i<n-2;i++){
        for(int j=0;j<n-1;j++){
            if(a[i][j]==0)break;
            int x=a[i][j];
            for(int k=0;k<n-1;k++){
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans)ans=c[i]+c[a[i][j]]+c[a[x][k]];
            }
        }
    }
    if(ans==che)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];

    long long g[n]={};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                if(g[i]==0)g[i]=c[i]+c[j];
                else if(c[i]+c[j]<g[i])g[i]=c[i]+c[j];
            }
        }
    }
    long long ans=3000000001;
    long long che=ans;
    for(int i=0;i<n-2;i++){
        for(int j=i+1;j<n-1;j++){
            if(s[j]>s[i])
            if(c[i]+g[j]<ans && g[j]!=0)ans=c[i]+g[j];
        }
    }
    if(ans==che)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={};
    long long g[n]={};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
                if(g[i]==0)g[i]=c[i]+c[j];
                else if(c[i]+c[j]<g[i])g[i]=c[i]+c[j];
            }
        }
    }
    long long ans=3000000001;
    long long che=ans;
    for(int i=0;i<n-2;i++){
/*        for(int j=0;j<n-1;j++){
            if(a[i][j]==0)break;
            int x=a[i][j];
            if(g[x]!=0)if(ans>c[i]+g[x])ans=c[i]+g[x];
            for(int k=0;k<n-1;k++){
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans){ans=c[i]+c[a[i][j]]+c[a[x][k]];}
            }
        }*/
        for(int j=i+1;j<n-1;j++){
            if(s[j]>s[i])
            if(c[i]+g[j]<ans && g[j]!=0)ans=c[i]+g[j];
        }
    }
    if(ans==che)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={0};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
            }
            else a[i][l++]=0;
        }
    }

    long long ans=3000000001;
    long long che=ans;
    for(int i=0;i<n-2;i++){
        for(int j=0;j<n-1;j++){
            if(a[i][j]==0)break;
            int x=a[i][j];
            for(int k=0;k<n-1;k++){
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans)ans=c[i]+c[a[i][j]]+c[a[x][k]];
            }
        }
    }
    if(ans==che)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={0};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
            }
        }
    }
    long long ans=3000000001,check;check=ans;
    for(int i=0;i<n-2;i++){
        for(int j=0;j<n-1;j++){
            if(a[i][j]==0 )break;
            int x=a[i][j];
            for(int k=0;k<n-1;k++){
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans)ans=c[i]+c[a[i][j]]+c[a[x][k]];

            }
        }
    }
    if(ans==check)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int s[n],c[n];
    for(int i=0;i<n;i++){cin>>s[i];}
    for(int i=0;i<n;i++)cin>>c[i];
    int a[n][n-1]={0};
    for(int i=0;i<n-1;i++){
        int l=0;
        for(int j=i+1;j<n;j++){
            if(s[i]<s[j]){
                a[i][l]=j;l++;
            }
        }
    }
    long long ans=3000000001,check;check=ans;
    for(int i=0;i<n-2;i++){
        for(int j=0;j<n-1;j++){
            if(a[i][j]==0 )break;
            int x=a[i][j];
            for(int k=0;k<n-1;k++){
                cout<<x<<" ";
                if(a[x][k]==0)break;
                if((c[i]+c[a[i][j]]+c[a[x][k]])<ans)ans=c[i]+c[a[i][j]]+c[a[x][k]];

            }
        }
    }
    if(ans==check)cout<<"-1"<<endl;
    else cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN],bg[MAXN],v[MAXN];
const ll inf=1e18;

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i], bg[i]=N+1;
    repi(i,1,N) cin>>c[i];
    repi(i,1,N) repi(j,i+1,N)
        if (s[j]>s[i]) {
            bg[i]=j;
            break;
        }
    ll ans=inf;
    memset(v,0,sizeof v);
    repi(i,1,N) {
        if (v[i]) continue;
        v[i]=1;
        priority_queue<int,vi,greater<int>> pq;
        pq.push(c[i]);
        int j=bg[i];
        while (j<N+1) pq.push(c[j]), v[j]=1, j=bg[j];
        if (pq.size()<3) continue;
        ll at=0ll;
        rep(k,0,3) at+=pq.top(), pq.pop();
        ans=min(ans,at);
    }
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN];
ll memo[MAXN][MAXN][4];
const ll inf=1e17;

ll dp(int i, int l, int k) {
    if (i==N+1) return (k==0?0ll:inf);
    if (memo[i][l][k]==-1) {
        memo[i][l][k]=dp(i+1,l,k);
        if (k&&(!l||s[i]>s[l])) memo[i][l][k]=min(memo[i][l][k],dp(i+1,i,k-1)+c[i]);
    }
    return memo[i][l][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=dp(1,0,3);
    print((ans==inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 100

int N,s[MAXN],c[MAXN],bg[MAXN],v[MAXN];
const ll inf=1e18;

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i], bg[i]=N+1;
    repi(i,1,N) cin>>c[i];
    repi(i,1,N) repi(j,i+1,N)
        if (s[j]>s[i]) {
            bg[i]=j;
            break;
        }
    ll ans=inf;
    memset(v,0,sizeof v);
    repi(i,1,N) {
        if (v[i]) continue;
        priority_queue<int,vi,greater<int>> pq;
        pq.push(c[i]);
        int j=bg[i];
        while (j<N+1) pq.push(c[j]), j=bg[j];
        if (pq.size()<3) continue;
        ll at=0ll;
        rep(k,0,3) at+=pq.top(), pq.pop();
        ans=min(ans,at);
    }
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN],bg[MAXN],v[MAXN];
const ll inf=1e18;

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i], bg[i]=N+1;
    repi(i,1,N) cin>>c[i];
    repi(i,1,N) repi(j,i+1,N)
        if (s[j]>s[i]) {
            bg[i]=j;
            break;
        }
    ll ans=inf;
    memset(v,0,sizeof v);
    repi(i,1,N) {
        if (v[i]) continue;
        priority_queue<int,vi,greater<int>> pq;
        pq.push(c[i]);
        int j=bg[i];
        while (j<N+1) pq.push(c[j]), j=bg[j];
        if (pq.size()<3) continue;
        ll at=0ll;
        rep(k,0,3) at+=pq.top(), pq.pop();
        ans=min(ans,at);
    }
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

vi bg[MAXN];
int N,s[MAXN],c[MAXN];
ll memo[MAXN][2][4];
const ll inf=1e17;

ll dp(int i, int t, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][t][k]==-1) {
        if (!t) memo[i][t][k]=min(dp(i+1,0,k),dp(i+1,1,k));
        else {
            memo[i][t][k]=dp(N+1,1,k-1)+c[i];
            for (int j:bg[i]) memo[i][t][k]=min(memo[i][t][k],dp(j,1,k-1)+c[i]);
        }
    }
    return memo[i][t][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    repi(i,1,N) repi(j,i+1,N) if (s[j]>s[i]) bg[i].pb(j);
    memset(memo,-1,sizeof memo);
    ll ans=min(dp(1,1,3),dp(1,0,3));
    print((ans==inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN],v[MAXN];
const ll inf=1e18;

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    ll ans=inf;
    repi(i,1,N) {
        priority_queue<int,vi,greater<int>> pq;
        pq.push(c[i]);
        int k=i;
        repi (j,i+1,N) if (s[k]<s[j]) pq.push(c[j]), k=j;
        if (pq.size()<3) continue;
        ll at=0ll;
        rep(k,0,3) at+=pq.top(), pq.pop();
        ans=min(ans,at);
    }
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN];
ll memo[MAXN][2][4];
const ll inf=1e17;

ll dp(int i, int t, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][t][k]==-1) {
        if (!t) memo[i][t][k]=dp(i+1,0,k);
        else {
            memo[i][t][k]=inf;
            repi(j,i+1,N) if (s[j]>s[i]) memo[i][t][k]=min(memo[i][t][k],dp(j,1,k-1)+c[i]);
        }
    }
    return memo[i][t][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=min(dp(1,1,3),dp(1,0,3));
    print((ans==inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 100

int N,s[MAXN],c[MAXN];
ll memo[MAXN][4];
const ll inf=1e15;

ll dp(int i, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][k]==-1) {
        memo[i][k]=inf;
        repi(j,i+1,N) if (s[j]>s[i]) memo[i][k]=min(memo[i][k],dp(j,k-1)+c[i]);
    }
    return memo[i][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=inf;
    rep(i,1,N) ans=min(ans,dp(i,3));
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN];
ll memo[MAXN][4];
const ll inf=1e15;

ll dp(int i, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][k]==-1) {
        memo[i][k]=inf;
        repi(j,i+1,N) if (s[j]>s[i]) memo[i][k]=min(memo[i][k],dp(j,k-1)+c[i]);
    }
    return memo[i][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=inf;
    rep(i,1,N) ans=min(ans,dp(i,3));
    print((ans>=inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 100

int N,s[MAXN],c[MAXN];
ll memo[MAXN][MAXN][4];
const ll inf=1e17;

ll dp(int i, int l, int k) {
    if (i==N+1) return (k==0?0ll:inf);
    if (memo[i][l][k]==-1) {
        memo[i][l][k]=dp(i+1,l,k);
        if (k&&(!l||s[i]>s[l])) memo[i][l][k]=min(memo[i][l][k],dp(i+1,i,k-1)+c[i]);
    }
    return memo[i][l][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=dp(1,0,3);
    print((ans==inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN];
ll memo[MAXN][2][4];
const ll inf=1e17;

ll dp(int i, int sq, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][sq][k]==-1) {
        if (!sq) memo[i][sq][k]=dp(i+1,0,k);
        else memo[i][sq][k]=inf;
        repi(j,i+1,N) if (s[j]>s[i]) memo[i][sq][k]=min(memo[i][sq][k],dp(j,1,k-1)+c[i]);
    }
    return memo[i][sq][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=dp(1,0,3);
    print((ans==inf?-1ll:ans));
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
#define mp make_pair
#define pb push_back
#define pf push_front
#define ins insert
#define print(x) cout<<x<<endl
#define debug(x) cerr<<#x<<" = "<<x<<endl
#define rep(i,s,N) for(int i=s;i<N;i++)
#define repi(i,s,N) for(int i=s;i<=N;i++)
#define MAXN 3001

int N,s[MAXN],c[MAXN];
ll memo[MAXN][2][4];
const ll inf=1e17;

ll dp(int i, int t, int k) {
    if (!k) return 0ll;
    if (i==N+1) return inf;
    if (memo[i][t][k]==-1) {
        if (!t) memo[i][t][k]=dp(i+1,0,k);
        else {
            memo[i][t][k]=dp(N+1,1,k-1)+c[i];
            repi(j,i+1,N) if (s[j]>s[i]) memo[i][t][k]=min(memo[i][t][k],dp(j,1,k-1)+c[i]);
        }
    }
    return memo[i][t][k];
}

int main() {
    cin>>N;
    repi(i,1,N) cin>>s[i];
    repi(i,1,N) cin>>c[i];
    memset(memo,-1,sizeof memo);
    ll ans=min(dp(1,1,3),dp(1,0,3));
    print((ans==inf?-1ll:ans));
}


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

int main()
{
	long long int n,cost=1e18;
	cin>>n;
	long long int a[n],i,j,k,b[n];
	for(i=0;i<n;++i)
	cin>>a[i];
	for(i=0;i<n;++i)
	cin>>b[i];
	for(i=0;i<n;++i)
	{
	    long long int at=1e18,bt=1e18;
	    for(j=0;j<i;++j)
	    if(a[j]<a[i])
	    at=min(at,b[i]);
    	for(k=i+1;k<n;++k)
    	if(a[i]<a[k])
    	bt=min(bt,b[i]);
    	if(at!=1e18&&bt!=1e18)
    	cost=min(at+bt+b[i],cost);
	}

	if(cost==1e18)
	cout<<"-1";
	else
	cout<<cost;
	return 0;
}


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

long max(long a,long b)
{
if(a>b)
return a;
return b;
}

long min(long a,long b)
{
if(a<b)
return a;
return b;
}

int main( )
{
long n,c,d,m=1000000000;
cin>>n;
long s[n],i,j,k;
for(i=0;i<n;++i)
cin>>s[i];
for(j=1;j<n-1;++j)
{
c=s[j];
d=s[j];
for(i=0;i<j;++i)
if(s[i]<s[j])
c=min(s[i],c);
for(k=j+1;k<n;++k)
if(s[j]<s[k])
d=min(d,s[k]);
if(s[j]+c+d<m)
m=c+d+s[j];
}
cout<<m;
return 0;
}


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

int main()
{
	long long int n,cost=1e18;
	cin>>n;
	long long int a[n],i,j,k,b[n];
	for(i=0;i<n;++i)
	cin>>a[i];
	for(i=0;i<n;++i)
	cin>>b[i];
	for(i=0;i<n;++i)
	{
	    long long int at=1e18,bt=1e18;
	    for(j=0;j<i;++j)
	    if(a[j]<a[i])
	    at=min(at,b[j]);
    	for(k=i+1;k<n;++k)
    	if(a[i]<a[k])
    	bt=min(bt,b[k]);
    	if(at!=1e18&&bt!=1e18)
    	cost=min(at+bt+b[i],cost);
	}

	if(cost==1e18)
	cout<<"-1";
	else
	cout<<cost;
	return 0;
}


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

long max(long a,long b)
{
if(a>b)
return a;
return b;
}

long min(long a,long b)
{
if(a<b)
return a;
return b;
}

int main( )
{
int n,c,d;
long m=1000000000;
cin>>n;
int s[i],i,j,k;
for(i=0;i<n;++i)
cin>>s[i];
for(j=1;j<n-1;++j)
{
c=s[j];
d=s[j];
for(i=0;i<j;++i)
if(s[i]<s[j])
c=min(s[i],c);
for(k=j+1;k<n;++k)
d=min(d,s[i]);
if(s[j]+c+d<m)
m=c+d+s[i];
}
cout<<m;
return 0;
}


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

int main()
{
	long long n,cost=1e18;
	cin>>n;
	long long a[n],i,j,k,b[n];
	for(i=0;i<n;++i)
	cin>>a[i];
	for(i=0;i<n;++i)
	cin>>b[i];
	for(i=0;i<n;++i)
	for(j=0;j<i;++j)
	for(k=i+1;k<n;++k)
	if(a[j]<a[i]&&a[i]<a[k])
	cost=min(b[j]+b[i]+b[k],cost);

	if(cost==1e18)
	cout<<"-1";
	else
	cout<<cost;
	return 0;
}


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

long max(long a,long b)
{
if(a>b)
return a;
return b;
}

long min(long a,long b)
{
if(a<b)
return a;
return b;
}

int main( )
{
long n,c,d,m=1000000000;
cin>>n;
long s[i],i,j,k;
for(i=0;i<n;++i)
cin>>s[i];
for(j=1;j<n-1;++j)
{
c=s[j];
d=s[j];
for(i=0;i<j;++i)
if(s[i]<s[j])
c=min(s[i],c);
for(k=j+1;k<n;++k)
if(s[j]<s[k])
d=min(d,s[k]);
if(s[j]+c+d<m)
m=c+d+s[j];
}
cout<<m;
return 0;
}


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

long max(long a,long b)
{
if(a>b)
return a;
return b;
}

long min(long a,long b)
{
if(a<b)
return a;
return b;
}

int main( )
{
long n,c,d,m=1000000000;
cin>>n;
long s[n],i,j,k;
for(i=0;i<n;++i)
cin>>s[i];
for(j=1;j<n-1;++j)
{
c=0;
d=0;
for(i=0;i<j;++i)
if(s[i]<s[j])
c=min(s[i],c);
for(k=j+1;k<n;++k)
if(s[j]<s[k])
d=min(d,s[k]);
if(s[j]+c+d<m)
m=c+d+s[j];
}
cout<<m;
return 0;
}


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

int main()
{
	long n,cost=3*1e9;
	cin>>n;
	long a[n],i,j,k,b[n];
	for(i=0;i<n;++i)
	cin>>a[i];
	for(i=0;i<n;++i)
	cin>>b[i];
	for(i=0;i<n;++i)
	for(j=0;j<i;++j)
	for(k=i+1;k<n;++k)
	if(a[j]<a[i]&&a[i]<a[k])
	cost=min(b[j]+b[i]+b[k],cost);

	if(cost==1e18)
	cout<<"-1";
	else
	cout<<cost;
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
int r,maxx,ans,minn = INT_MAX,ma,b[1000003];;
int main() {
	ios::sync_with_stdio(false);

       int n;
       cin >> n;

       for (int i = 1; i<=n; i++){
              int x,y;
              cin >> x >> y;
              minn = min(minn,x);
              maxx = max(maxx,x);
              b[x] = y;
       }
       for (int i = minn; i <= maxx; i++){
              if (r > 0) r--;
              r+=b[i];
              ma = max(ma,r);
       }
       ans+=maxx + r;
       cout<<ans<<" "<<ma<<endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
int r,maxx,ans;
int main() {
	ios::sync_with_stdio(false);

       int n;
       cin >> n;
       for (int i =1;i<=n; i++){
              int a,b;
              cin >> a >> b;
              if (r > 0) r--;
              r+=b;
              ans = a;
              maxx = max(maxx,r);
       }

       cout<<ans + r<<" "<<maxx<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ff first
#define ss second
const int INF=1000*1007;
const int MOD=1e9 + 7;
const int MAXN=100100;
char ans[MAXN],s[MAXN];
int main() {
    ios_base::sync_with_stdio(false);
    ll n;
    cin>>n;
    ll t=0,q=0,m=0;
    for(ll i=0;i<n;i++){
        ll a,b;
        cin>>a>>b;
        q=q-min(q,a-t)+b;
        t=a;
        m=max(m,q);
    }
    cout<<t+q<<" "<<m;
    return 0;
}


#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
const int maxn = 2e5+10;

int n;
int t[maxn],c[maxn];

int main()
{
    IO;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>t[i]>>c[i];
    int maxA = c[0], cnt = c[0];
    for(int i=1; i<n; i++)
    {
        if(t[i-1] + cnt<=t[i])
            cnt = c[i];
        else
            cnt += c[i] - t[i] + t[i-1];
        maxA = max(maxA,cnt);
    }
    cout<<t[n-1] + cnt<<" "<<maxA<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
const int maxn = 2e5+10;

int n;
int t[maxn],c[maxn];

int main()
{
    IO;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>t[i]>>c[i];
    int time = t[0], maxA = c[0], cnt = c[0];
    for(int i=1; i<n; i++)
    {
        if(time + cnt<=t[i])
        {
            time = t[i];
            cnt = c[i];
            maxA = max(maxA,cnt);
        }
        else
        {
            cnt = c[i] + cnt - t[i] + time;
            maxA = max(maxA,cnt);
            time = t[i];
        }
    }
    cout<<time + cnt<<" "<<maxA<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef long double ldb;
typedef pair<int,int> pii;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    queue<int> q;
    int a = 0, b = 0;
    int t, c;
    cin>>t>>c;
    for(int i = 1; i < 2000009; ++i)
    {
        if(!q.empty())
        {
            q.pop();
            a = i;
        }
        while(t<=i && n)
        {
            while(c--)
                q.push(1);
            if(--n)
                cin>>t>>c;
        }
        b = max(b, int(q.size()));
    }
    cout<<a<<' '<<b<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned long long uLL;
typedef long double ldb;
typedef pair<int,int> pii;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    LL q = 0;
    LL a = 0, b = 0;
    LL t, c;
    cin>>t>>c;
    for(LL i = 1;; ++i)
    {
        if(q)
        {
            LL l = min(t-i, q);
            if(l)
            {
                i += l-1;
                q -= l-1;
            }
        }
        if(q)
        {
            --q;
            a = i;
        }
        while(t<=i && n)
        {
            q += c;
            if(--n)
                cin>>t>>c;
            else
                t = 1e18;
        }
        b = max(b, q);
        if(!n && !q)
            break;
    }
    cout<<a<<' '<<b<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define N 2000010
#define inf 10000000000000000
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
//ll dx[]={1,0,-1,0};ll dy[]={0,1,0,-1}; //4 Direction
//ll dx[]={1,1,0,-1,-1,-1,0,1};ll dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int expo(int n,int m,int p)   //MODULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
        if(m%2)
            r=(r*n)%p;
        n=(n*n)%p;
        m=m/2;
    }
    return r%p;
}
int32_t main()
{

   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ************************/
   int n;
   cin>>n;
   int a1=0,t=0,d=0,T=0;
   /*if(n==1)
   {
   	int x,y;
   	cin>>x>>y;
   	cout<<x+y<<' '<<y<<endl;
   	return 0;
   }*/
   while(n--)
   {
   	int x,y;
   	cin>>x>>y;
   	while(t!=x&&d!=0)
   		d--,t++;
   	t=x;
   	a1=max(a1,d);
   	d=d+y;
   	a1=max(a1,d);
   }
   T=t+d;
   cout<<T<<' '<<a1;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define MOD 1000000007
#define pi pair<int,int>
#define gcd(a,b) __gcd((a),(b))
#define endl "\n"
#define UB upper_bound
#define LB lower_bound
#define BS binary_search
#define PI 3.1415926535897932384626433832795

const int maxn = 2e5+10;

int n;
int t[maxn],c[maxn];

int main(){
   /* #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif*/
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    for(int i=0; i<n; i++)
        cin>>t[i]>>c[i];
    int maxA = c[0], cnt = c[0];
    for(int i=1; i<n; i++)
    {
        if(t[i-1] + cnt<=t[i])
            cnt = c[i];
        else
            cnt += c[i] - t[i] + t[i-1];
        maxA = max(maxA,cnt);
    }
    cout<<t[n-1] + cnt<<" "<<maxA<<endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#pragma comment(linker, "/STACK:32000000")
#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <cassert>
#include <fstream>
#include <map>
#include <unordered_map>
#include <deque>

using namespace std;

#define inf 1000000007
#define eps 1e-7
#define mp(a, b) make_pair(a, b)
#define llinf 2000000000000000008LL

typedef long long ll;
typedef unsigned u;
typedef long double ld;
typedef unsigned char uc;
typedef unsigned long long ull;

int n;
int add[1000005];
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(0);
	cout.sync_with_stdio(0);
	cout.precision(8);
	srand(time(0));
	cin >> n;
	for (int i = 0; i < n; i++) {
		int t, c;
		cin >> t >> c;
		add[t] += c;
	}
	int maxsz = 0, maxtime = 0, t = 0;
	for (int i = 0; i < 1000005; i++) {
		t = max(0, t - 1);
		t += add[i];
		if (t) {
			maxsz = max(maxsz, t);
			maxtime = max(maxtime, i + t);
		}
	}
	cout << maxtime << " " << maxsz;
	return 0;
}


#include<iostream>
using namespace std;
int n,t,c;
long long m,s,maxn;
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>t>>c;
		s-=t-m;
		s=max(s,0ll);
		s+=c;
		maxn=max(maxn,s);
		m=t;
	}
	cout<<m+s<<" "<<maxn<<endl;
	return 0;
}


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

int n, t, c, prv, len, ans1, ans2;

int main() {
	cin >> n;
	while (n--) {
		scanf("%d %d", &t, &c);
		len = max(0, len - t + prv) + c;
		prv = t;
		ans1 = max(ans1, t) + c;
		ans2 = max(ans2, len);
	}
	cout << ans1 << ' ' << ans2;
	return 0;
}