#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+10,M=1e6+10,inf=1e9+10;
const ll INF=1e18+10,mod=2147493647;
struct is
{
    int v;
    int next;
}edge[N<<1];
int head[N<<1],edg;
ll sum[N],a[N],dp[N][5];
void init()
{
    memset(head,-1,sizeof(head));
    edg=0;
}
void add(int u,int v)
{
    edg++;
    edge[edg].v=v;
    edge[edg].next=head[u];
    head[u]=edg;
}
void dfs(int u,int fa)
{
    sum[u]=a[u];
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        dfs(v,u);
        sum[u]+=sum[v];
    }
}
void dfs1(int u,int fa)
{
    //cout<<u<<" "<<fa<<endl;
    int flag=1;
    priority_queue<ll,vector<ll>,greater<ll> >q;
    ll ans2=-INF,ans1=-INF;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa)continue;
        dfs1(v,u);
        if(dp[v][1]!=sum[v])flag=0;
        ans2=max(ans2,dp[v][2]);
        ans1=max(ans1,dp[v][1]);
        q.push(dp[v][1]);
        while(q.size()>2)
        {
            q.pop();
        }
    }
    dp[u][1]=max(dp[u][1],ans1);
    ll s1=-INF,s2=-INF,p=1,s3;
    while(!q.empty())
    {
        if(p==1)
            s1=q.top();
        else
            s2=q.top();
            q.pop();
        p++;
    }
    if(s2==-INF)
        s3=-INF;
    else
        s3=s2+s1;
    dp[u][2]=max(dp[u][2],ans2);
    dp[u][2]=max(dp[u][2],s3);
    if(flag) dp[u][1]=max(dp[u][1],sum[u]);
}
int main()
{
    init();
    int n;
    scanf("%d",&n);
    for(int i=0;i<=n;i++)
        dp[i][1]=dp[i][2]=-INF;
    for(int i=1;i<=n;i++)
    scanf("%lld",&a[i]);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    if(n<=2)
    {
        return puts("Impossible\n");
    }
    dfs(1,-1);
    dfs1(1,-1);
    //for(int i=1;i<=n;i++)
        //cout<<dp[i][1]<<" "<<dp[i][2]<<endl;
    if(dp[1][2]>-INF)
    printf("%lld\n",dp[1][2]);
    else
        puts("Impossible\n");
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 5;
vector <int> tree[MAXN];
int joy[MAXN];
long long int sumComponents[MAXN];
long long int sum[MAXN];
bool used[MAXN];
bool usedComponents[MAXN];
int components = 0;

void countSum(int v, int component) {
    used[v] = true;
    long long int currentSum = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if (!used[newV]) {
            countSum(newV, component);
            currentSum += sum[newV];
        }
    }

    sum[v] = currentSum + joy[v];
    if (!usedComponents[component] || sumComponents[component] < sum[v]) {
        usedComponents[component] = true;
        sumComponents[component] = sum[v];
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
        usedComponents[i] = false;
    }

    int root = 1;
    used[root] = true;
    while (tree[root].size() == 1) {
        used[root] = true;
        root = tree[root][0];
    }

    if (tree[root].size() == 0) {
        cout << "Impossible" << endl;
        return 0;
    }

    for (size_t t = 0; t < tree[root].size(); t++) {
        countSum(tree[1][t], t);
    }
    int cnt = tree[root].size();
    sort(sumComponents, sumComponents + cnt);
    cout << sumComponents[cnt - 1] + sumComponents[cnt - 2] << endl;

    //int size

    //for (int i = 1; i <= n; i++)
    //    cout << sum[i] << " ";

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 15 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!t || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if (!used[newV]) {
            if (newV == vertex)
                dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
            else
                dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));

        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    bool isSet = false;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSet || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
            isSet = true;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;

    if (!newHasSecond) {
        if (tree[v].size() >= 1) {
            for (size_t t = 0; t < tree[v].size(); t++)
                if (!used[tree[v][t]])
                    dfsTwo(tree[v][t], false, 0);
        }
    } else {

        for (size_t t = 0; t < tree[v].size(); t++) {
            int newV = tree[v][t];
            if (!used[newV]) {
                if (newV == vertex)
                    if (hasSecond && isSetMax)
                        dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
                    else if (isSetMax)
                        dfsTwo(newV, newHasSecond, secondMax);
                    else
                        dfsTwo(newV, newHasSecond, maxSecond);
                else
                    if (hasSecond && isSet)
                        dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));
                    else if (isSet)
                        dfsTwo(newV, newHasSecond, currentMax);
                    else
                        dfsTwo(newV, newHasSecond, maxSecond);
            }
        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 15 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    bool isSet = false;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSet || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
            isSet = true;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;

    if (!newHasSecond) {
        if (tree[v].size() > 1)
            dfsTwo(tree[v][0], false, 0);
    } else {

        for (size_t t = 0; t < tree[v].size(); t++) {
            int newV = tree[v][t];
            if (!used[newV]) {
                if (newV == vertex)
                    if (hasSecond)
                        dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
                    else
                        dfsTwo(newV, newHasSecond, secondMax);
                else
                    if (hasSecond)
                        dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));
                    else
                        dfsTwo(newV, newHasSecond, currentMax);
            }
        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 15 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    bool isSet = false;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSet || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
            isSet = true;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;

    if (!newHasSecond) {
        if (tree[v].size() >= 1) {
            for (size_t t = 0; t < tree[v].size(); t++)
                if (!used[tree[v][t]])
                    dfsTwo(tree[v][t], false, 0);
        }
    } else {

        for (size_t t = 0; t < tree[v].size(); t++) {
            int newV = tree[v][t];
            if (!used[newV]) {
                if (newV == vertex)
                    if (hasSecond)
                        dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
                    else
                        dfsTwo(newV, newHasSecond, secondMax);
                else
                    if (hasSecond)
                        dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));
                    else
                        dfsTwo(newV, newHasSecond, currentMax);
            }
        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 15 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    bool isSet = false;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSet || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
            isSet = true;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;

    if (!newHasSecond) {
        if (tree[v].size() >= 1) {
            for (size_t t = 0; t < tree[v].size(); t++)
                if (!used[tree[v][t]])
                    dfsTwo(tree[v][t], false, 0);
        }
    } else {

        for (size_t t = 0; t < tree[v].size(); t++) {
            int newV = tree[v][t];
            if (!used[newV]) {
                if (newV == vertex)
                    if (hasSecond && isSetMax)
                        dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
                    else if (isSetMax)
                        dfsTwo(newV, newHasSecond, secondMax);
                    else
                        dfsTwo(newV, newHasSecond, maxSecond);
                else
                    if (hasSecond && isSet)
                        dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));
                    else if (isSet)
                        dfsTwo(newV, newHasSecond, currentMax);
                    else
                        dfsTwo(newV, newHasSecond, maxSecond);
            }
        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 5;
bool isResult = false;
long long int ans = 0;
vector <int> tree[MAXN];
vector <int> tree1[MAXN];
int joy[MAXN];
long long int sumComponents[MAXN];
long long int sum[MAXN];
bool used[MAXN];
bool usedComponents[MAXN];
int components = 0;

void countSum(int v, int component) {
    used[v] = true;
    long long int currentSum = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if (!used[newV]) {
            countSum(newV, component);
            currentSum += sum[newV];
            tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (tree1[v].size() >= 2) {
        int s = tree1[v].size();
        sort(tree1[v].begin(), tree1[v].end());
        if (!isResult || ans < tree1[v][s - 1] + tree1[v][s - 2])
        {
            ans = tree1[v][s - 1] + tree1[v][s - 2];
            isResult = true;
        }
    }

    if (!usedComponents[component] || sumComponents[component] < sum[v]) {
        usedComponents[component] = true;
        sumComponents[component] = sum[v];
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
        usedComponents[i] = false;
    }

    int root = 1;
    used[root] = true;
    while (tree[root].size() == 1) {
        used[root] = true;
        root = tree[root][0];
    }

    if (tree[root].size() == 0) {
        cout << "Impossible" << endl;
        return 0;
    }

    for (size_t t = 0; t < tree[root].size(); t++) {
        countSum(tree[1][t], t);
    }

    cout << ans << endl;

    //int size

    //for (int i = 1; i <= n; i++)
    //    cout << sum[i] << " ";

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAXN = 2e5 + 5;

bool isResult = false;
long long int ans = 0;

vector <int> tree[MAXN];
//vector <int> tree1[MAXN];

int joy[MAXN];
long long int sum[MAXN];
long long int mx[MAXN];

bool used[MAXN];

void dfs(int v) {
    used[v] = true;
    long long int currentSum = 0, maxSubTree = 0;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];

        if (!used[newV]) {
            dfs(newV);
            if (currentSum == 0 || maxSubTree < mx[newV]) {
                maxSubTree = mx[newV];
            }

            currentSum += sum[newV];
            //tree1[v].push_back(sum[newV]);
        }
    }

    sum[v] = currentSum + joy[v];
    if (currentSum == 0 || sum[v] > maxSubTree) {
        maxSubTree = sum[v];
    }
    mx[v] = maxSubTree;
}

void dfsTwo(int v, bool hasSecond, long long int maxSecond) {
    used[v] = true;
    long long int currentMax = 0, secondMax = 0;
    int vertex = 0;
    bool isSetMax = false;
    if (hasSecond) {
        if (!isResult || ans < maxSecond + mx[v]) {
            isResult = true;
            ans = maxSecond + mx[v];
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!t || currentMax < mx[newV]) && !used[newV]) {
            currentMax = mx[newV];
            vertex = newV;
        }
    }

    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if ((!isSetMax || secondMax < mx[newV]) && newV != vertex && !used[newV]) {
            secondMax = mx[tree[v][t]];
            isSetMax = true;
        }
    }

    bool newHasSecond = hasSecond || isSetMax;
    for (size_t t = 0; t < tree[v].size(); t++) {
        int newV = tree[v][t];
        if (!used[newV]) {
            if (newV == vertex)
                if (hasSecond)
                    dfsTwo(newV, newHasSecond, max(maxSecond, secondMax));
                else
                    dfsTwo(newV, newHasSecond, secondMax);
            else
                if (hasSecond)
                    dfsTwo(newV, newHasSecond, max(maxSecond, currentMax));
                else
                    dfsTwo(newV, newHasSecond, currentMax);
        }
    }
}

int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> joy[i];

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfs(1);

    for (int i = 1; i <= n; i++) {
        used[i] = false;
    }
    dfsTwo(1, false, 0);

    if (!isResult)
        cout << "Impossible" << endl;
    else
        cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define f cin
#define g cout
#define ll long long
#define NMax 200001
#define INF 2000000000000000000
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        g << "Impossible" << '\n';
    }else
        g << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define NMax 200001
#define INF 1000000000000000001
#define ll long long
#define f cin
#define g cout
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,m,x,y,ans;
vector<ll> G[NMax];
ll F[NMax],S[NMax],Sum[NMax],viz[NMax],a[NMax],SI[NMax],FI[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = max(F[G[nod][i]],Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
void dfs2(ll nod,ll mx){
    viz[nod] = 1;
    if(nod != 1){
        ans = max(ans,mx + Sum[nod]);
      //  g << nod << ' ' << mx << ' ' << Sum[nod] << ' ' <<mx + Sum[nod] << '\n';
    }
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            if(G[nod][i] != FI[nod]){
                dfs2(G[nod][i],max(mx,F[nod]));
            }else{
                dfs2(G[nod][i],max(mx,S[nod]));
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    memset(viz,0,sizeof(viz));
    ans = -INF;
    dfs2(1,-INF);
    if(ans != -INF)
        g << ans << '\n';
    else
        g << "Impossible" << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define f cin
#define g cout
#define ll long long
#define NMax 200001
#define INF 1000000000000000000
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(F[G[nod][i]] + a[G[nod][i]] > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        g << "Impossible" << '\n';
    }else
        g << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

//#define f cin
//#define g cout
#define ll long long
#define NMax 200001
#define INF 2000000000000000000
using namespace std;

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

#define DIM 10000
char buff[DIM];
ll poz=0;

void cit(ll &numar)
{
     numar = 0;
     char semn='+';
     while (buff[poz] < '0' || buff[poz] > '9')
     {
          semn = buff[poz];
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     if (semn == '-')
          numar = -numar;
}
void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            if(max(F[G[nod][i]],Sum[G[nod][i]]) > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = max(F[G[nod][i]],Sum[G[nod][i]]);
                FI[nod] = G[nod][i];
            }else{
                if(max(F[G[nod][i]],Sum[G[nod][i]]) > S[nod]){
                    S[nod] = max(F[G[nod][i]],Sum[G[nod][i]]);
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    cit(n);
    for(ll i = 1; i <= n; ++i){
        cit(a[i]);
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        cit(x);cit(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        if(mx != -INF)
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        printf("Impossible\n");
    }else
        printf("%I64d\n",ans);
    return 0;
}


#include <bits/stdc++.h>

//#define f cin
//#define g cout
#define ll long long
#define NMax 200001
#define INF 2000000000000000000
using namespace std;

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

#define DIM 10000
char buff[DIM];
ll poz=0;

void cit(ll &numar)
{
     numar = 0;
     char semn='+';
     while (buff[poz] < '0' || buff[poz] > '9')
     {
          semn = buff[poz];
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     while ('0'<=buff[poz] && buff[poz]<='9')
     {
          numar = numar*10 + buff[poz] - '0';
          if (++poz == DIM)
               fread(buff,1,DIM,stdin),poz=0;
     }
     if (semn == '-')
          numar = -numar;
}
void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    cit(n);
    for(ll i = 1; i <= n; ++i){
        cit(a[i]);
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        cit(x);cit(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        if(mx != -INF)
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        printf("Impossible\n");
    }else
        printf("%I64d\n",ans);
    return 0;
}


#include <bits/stdc++.h>

#define f cin
#define g cout
#define ll long long
#define NMax 200001
#define INF 2000000000000000000
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,x,y,ans = -INF,t;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    t++;
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    if(t < n){
        g << "Impossible" << '\n';
        return 0;
    }
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        if(mx != -INF)
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        g << "Impossible" << '\n';
    }else
        g << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

//#define f cin
//#define g cout
#define ll long long
#define NMax 200001
#define INF 1000000000000000000
using namespace std;
ifstream f("d.in");
ofstream g("d.out");

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(F[G[nod][i]] + a[G[nod][i]] > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        g << "Impossible" << '\n';
    }else
        g << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define NMax 200001
#define INF 1000000000000000001
#define ll long long
//#define f cin
//#define g cout
using namespace std;
ifstream f("d.in");
ofstream g("d.out");

ll n,m,x,y,ans;
vector<ll> G[NMax];
ll F[NMax],S[NMax],Sum[NMax],viz[NMax],a[NMax],SI[NMax],FI[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = max(F[G[nod][i]],Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
void dfs2(ll nod,ll mx){
    viz[nod] = 1;
    if(nod != 1){
        if(mx != -INF)
            ans = max(ans,mx + Sum[nod]);
      //  g << nod << ' ' << mx << ' ' << Sum[nod] << ' ' <<mx + Sum[nod] << '\n';
    }
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            if(G[nod][i] != FI[nod]){
                dfs2(G[nod][i],max(mx,F[nod]));
            }else{
                dfs2(G[nod][i],max(mx,S[nod]));
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    memset(viz,0,sizeof(viz));
    ans = -INF;
    dfs2(1,-INF);
    if(ans != -INF)
        g << ans << '\n';
    else
        g << "Impossible" << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define f cin
#define g cout
#define ll long long
#define NMax 200001
#define INF 1000000000000000000
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,x,y,ans = -INF;
ll a[NMax],Sum[NMax],F[NMax],S[NMax],tata[NMax],FI[NMax],SI[NMax];
bool viz[NMax];
vector<ll> G[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            tata[G[nod][i]] = nod;
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = F[G[nod][i]];
            mx = max(mx,Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(F[G[nod][i]] + a[G[nod][i]] > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    for(ll i = 2; i <= n; ++i){
        ll mx = -INF;
        ll e = tata[i];
        ll x = i;
        while(e){
            if(FI[e] == x){
                mx = max(mx,S[e]);
            }else{
                mx = max(mx,F[e]);
            }
            x = e;
            e = tata[e];
        }
        ans = max(ans,Sum[i] + mx);
    }
    if(ans == -INF){
        g << -1 << '\n';
    }else
        g << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

#define NMax 200001
#define INF 1000000000000000001
#define ll long long
#define f cin
#define g cout
using namespace std;
//ifstream f("d.in");
//ofstream g("d.out");

ll n,m,x,y,ans;
vector<ll> G[NMax];
ll F[NMax],S[NMax],Sum[NMax],viz[NMax],a[NMax],SI[NMax],FI[NMax];

void dfs(ll nod){
    viz[nod] = 1;
    Sum[nod] = a[nod];
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            dfs(G[nod][i]);
            Sum[nod] += Sum[G[nod][i]];

            ll mx = max(F[G[nod][i]],Sum[G[nod][i]]);

            if(mx > F[nod]){
                S[nod] = F[nod];
                SI[nod] = FI[nod];

                F[nod] = mx;
                FI[nod] = G[nod][i];
            }else{
                if(mx > S[nod]){
                    S[nod] = mx;
                    SI[nod] = G[nod][i];
                }
            }
        }
    }
}
void dfs2(ll nod,ll mx){
    viz[nod] = 1;
    if(nod != 1){
        if(mx != -INF)
            ans = max(ans,mx + Sum[nod]);
      //  g << nod << ' ' << mx << ' ' << Sum[nod] << ' ' <<mx + Sum[nod] << '\n';
    }
    for(ll i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            if(G[nod][i] != FI[nod]){
                dfs2(G[nod][i],max(mx,F[nod]));
            }else{
                dfs2(G[nod][i],max(mx,S[nod]));
            }
        }
    }
}
int main()
{
    f >> n;
    for(ll i = 1; i <= n; ++i){
        f >> a[i];
        F[i] = -INF;
        S[i] = -INF;
    }
    for(ll i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    memset(viz,0,sizeof(viz));
    ans = -INF;
    dfs2(1,-INF);
    if(ans != -INF)
        g << ans << '\n';
    else
        g << "Impossible" << '\n';
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
struct CITY{
	int from;
	int to;
	int next;
}p[400005];
long long n,d[200005],a,b,t,head[200005],f[200005][5],aerfa;
void add(int from,int to){
	t++;
	p[t].from=from;
	p[t].to=to;
	p[t].next=head[from];
	head[from]=t;
}

void dfs(int u,int fa){
	int where=head[u];
	f[u][0]=d[u];
	int flag=0;
	while(where!=0){
		int v=p[where].to;
		if(v==fa){
			where=p[where].next;
			continue;
		}
		flag++;
		dfs(v,u);
		if(max(f[v][0],f[v][1])>f[u][1]){
			f[u][2]=f[u][1];
			f[u][1]=max(f[v][0],f[v][1]);
		}else{
			if(max(f[v][0],f[v][1])>f[u][2]){
				f[u][2]=max(f[v][0],f[v][1]);
			}
		}
		f[u][0]+=f[v][0];
		where=p[where].next;
	}
	if(flag>1){
		aerfa=1;
	}
}
int main(){
	memset(f,-0x7f/2,sizeof(f));
	f[0][0]=0;
	f[0][1]=0;
	f[0][2]=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>d[i];
	}
	for(int i=1;i<n;i++){
		cin>>a>>b;
		add(a,b);
		add(b,a);
	}

	dfs(1,0);
	if(aerfa==0){
		cout<<"Impossible";
		return 0;
	}
	long long oo=-0x7f7f7f7f7f7f;
	for(int i=1;i<=n;i++){
		oo=max(oo,f[i][1]+f[i][2]);
		//cout<<i<<":"<<f[i][1]<<"  "<<f[i][2]<<endl;
	}
	cout<<oo;
	return 0;
}
/*
10
0 9 -5 1 3 -5 1 2 3 -1
1 2
1 3
1 4
5 2
6 2
3 7
3 9
3 8
4 10
*/


#include<stdio.h>
#define inf 1000000000
const long long maxn=500005,maxm=1000005;
long long i,j,k,m,n,e,ans=-inf;
long long val[maxn],start[maxn],to[maxm],then[maxm],dp[maxn],sum[maxn];
inline void add(long long x,long long y){
	then[++e]=start[x],start[x]=e,to[e]=y;
}
inline long long max(long long a,long long b){
	return a>b? a:b;
}
void dfs(long long x,long long last){
	dp[x]=-inf,sum[x]=val[x];
	for(long long i=start[x];i;i=then[i]){
		long long y=to[i];
		if(y==last)
			continue;
		dfs(y,x);
		ans=max(ans,dp[x]+dp[y]);
		sum[x]+=sum[y];
		dp[x]=max(dp[x],dp[y]);
	}
	dp[x]=max(dp[x],sum[x]);
}
int main(){
	scanf("%lld",&n);
	for(i=1;i<=n;i++)
		scanf("%lld",&val[i]);
	for(i=1;i<n;i++){
		long long x,y;
		scanf("%lld%lld",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(1,0);
	if(ans<=-inf)
		puts("Impossible");
	else printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MX=2e5+5;
const LL INF=99999999999999;
int read(){
	int x=0; char ch=getchar(); bool f=0;
	while('9'<ch||ch<'0') f|=(ch=='-'),ch=getchar();
	while('0'<=ch&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
struct edge{
	int to,ne;
}E[MX<<1];
int tot,head[MX],n;
inline void add(int a,int b){
	E[++tot]=(edge){b,head[a]};
	head[a]=tot;
}
LL f[MX][3],tr[MX];
void dp(int u,int fa){
	f[u][0]=tr[u];
	LL mx1=-INF,mx2=-INF;
	for(int i=head[u];i;i=E[i].ne){
		int to=E[i].to;
		if(to==fa) continue;
		dp(to,u);
		f[u][0]+=f[to][0];
		f[u][1]=max(f[u][1],max(f[to][1],f[to][0]));
		f[u][2]=max(f[u][2],f[to][2]);
		LL temp=max(f[to][1],f[to][0]);
		if(temp>mx1) mx2=mx1,mx1=temp;
		else if(temp>mx2) mx2=temp;
	}
	if(mx2!=-INF) f[u][2]=max(f[u][2],mx1+mx2);
}

int main(){
	int a,b;
	n=read();
	for(int i=1;i<=n;i++) f[i][0]=f[i][1]=f[i][2]=-INF;
	for(int i=1;i<=n;i++) tr[i]=read();
	for(int i=1;i<n;i++){
		a=read();b=read();
		add(a,b);add(b,a);
	}
	dp(1,0);
	if(f[1][2]!=-INF) printf("%lld",f[1][2]);
	else puts("Impossible");
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200005;
int n, w[N];
ll sum[N], ans=-0x3f3f3f3f3f3f3f3f;
ll a[N*4];
int tot=0, c[N], L[N], R[N];
vector<int> e[N];
void up(int now){
	a[now] = max(a[now<<1], a[now<<1|1]);
}
void build(int l, int r, int now){
	if (l == r){
		a[now] = sum[L[l]];
		return;
	}
	int mid = (l+r)>>1;
	build(l, mid, now<<1);
	build(mid+1, r, now<<1|1);
	up(now);
}
ll ask(int l, int r, int now, int s, int t){
	if (s <= l && r <= t) return a[now];
	int mid = (l+r)>>1;
	ll res = -0x3f3f3f3f3f3f3f3f;
	if (s <= mid) res = max(ask(l, mid, now<<1, s, t), res);
	if (t > mid) res = max(ask(mid+1, r, now<<1|1, s, t), res);
	return res;
}
void dfs1(int u, int fa){
	sum[u] = w[u];
	c[++tot] = u;
	L[u] = tot;
	for (int i = 0; i < e[u].size(); i++){
		int v = e[u][i];
		if (v == fa) continue;
		dfs1(v, u);
		sum[u] += sum[v];
	}
	R[u] = tot;
}
int main(){
	std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//	freopen("test_file/in.txt", "r", stdin);
//	freopen("test_file/out.txt", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> w[i];
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs1(1, 0);
	build(1, n, 1);
	for (int i = 1; i <= n; i++){
		if (R[i]+1 > n) continue;
		ans = max(ask(1, n, 1, R[i]+1, n)+sum[i], ans);
	}
	if (ans >= -1e15) cout << ans;
	else cout << "Impossible";
	return 0;
}


/*Heroes Never Die!*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 200006
typedef long long ll;
#define max(a,b) a>b?a:b
ll n;
ll w[MAXN];
struct edge{
    ll u;edge* next;
    edge(ll u=0,edge* next = NULL) : u(u),next(next) {}
    void insert(ll u){
        if(this->next == NULL )this ->next = new edge(u,NULL);
        else this->next = new edge(u,this->next);
    }
}e[MAXN];
ll maxs=-0x7f7f7f7f7f7f7f7f;
ll dfs(ll u,ll fa,ll& dp){
    ll siz = w[u];bool flag = 0;
    ll ans = -0x7f7f7f7f7f7f7f7f;dp = -0x7f7f7f7f7f7f7f7f;
    for(edge* n=e[u].next;n!=NULL;n=n->next){
        ll v = n->u;
        if(v == fa) continue;
        ll DP;
        siz += dfs(v,u,DP);
        if(dp!=-0x7f7f7f7f7f7f7f7f) ans = max(ans,dp + DP);
        dp = max(dp,DP);
    }
    dp = max(dp , siz);
    maxs = max(ans,maxs);
    return siz;
}
int main(void){
    cin >> n;
    for(ll i=1;i<=n;++i) scanf("%lld",&w[i]);
    for(ll i=0;i<n-1;++i){
        static ll u,v;
        scanf("%lld%lld",&u,&v);
        e[u].insert(v),e[v].insert(u);
    }
    ll ans;
    dfs(1,1,ans);
    if(maxs == -0x7f7f7f7f7f7f7f7f)
        cout << "Impossible";
    else cout << maxs;
}


#include<stdio.h>
#define inf 100000000000000000
const long long maxn=500005,maxm=1000005;
long long i,j,k,m,n,e,ans=-inf;
long long val[maxn],start[maxn],to[maxm],then[maxm],dp[maxn],sum[maxn];
inline void add(long long x,long long y){
	then[++e]=start[x],start[x]=e,to[e]=y;
}
inline long long max(long long a,long long b){
	return a>b? a:b;
}
void dfs(long long x,long long last){
	dp[x]=-inf,sum[x]=val[x];
	for(long long i=start[x];i;i=then[i]){
		long long y=to[i];
		if(y==last)
			continue;
		dfs(y,x);
		ans=max(ans,dp[x]+dp[y]);
		sum[x]+=sum[y];
		dp[x]=max(dp[x],dp[y]);
	}
	dp[x]=max(dp[x],sum[x]);
}
int main(){
	scanf("%lld",&n);
	for(i=1;i<=n;i++)
		scanf("%lld",&val[i]);
	for(i=1;i<n;i++){
		long long x,y;
		scanf("%lld%lld",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(1,0);
	if(ans<=-inf)
		puts("Impossible");
	else printf("%lld\n",ans);
	return 0;
}


#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
typedef long long int64;
const int N = 2e5+5;
const int64 INF = 0x3f3f3f3f3f3f3f3fLL;
int n, w[N];
vector<int> edge[N];
int64 f[N], sum[N], ans = -INF, dep[N];
void dfs(int x, int fa)
{
    sum[x] = w[x]; int64 d = 0; dep[x] = dep[fa]+1;
    for(int i = 0; i < edge[x].size(); ++i)
    {
        int y = edge[x][i];
        if(y == fa) continue;
        dfs(y, x);
        d = max(d, f[x]+f[y]);
        sum[x] += sum[y];
        f[x] = max(f[x], f[y]);
    }
    f[x] = max(f[x], sum[x]);
    if((!fa&&edge[x].size() > 1)||edge[x].size() > 2) ans = max(ans, d);
}
int main()
{
    n = read();
    for(int x = 1; x <= n; ++x) w[x] = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    dfs(1, 0);
    for(int x = 1; x <= n; ++x)
        if(dep[x] == n) return printf("Impossible")&0;
    printf("%lld\n", ans);
    return 0;
}


#include<iostream>
#define N 200005
using namespace std;
long long n,c[N];
struct ST{
	int from,to;
	int next;
}e[2*N];
int head[N],num;
int f[N],cm[N],size[N];
void add(int a,int b){
	num++;
	e[num].from=a;
	e[num].to=b;
	e[num].next=head[a];
	head[a]=num;
}
int sum(int x,int fa){
	f[x]=fa;size[x]=c[x];
	for(int i=head[x];i!=0;i=e[i].next){
		if(e[i].to==fa)continue;
		size[x]+=sum(e[i].to,e[i].from);
	}
	return size[x];
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>c[i];
	for(int i=1;i<n;i++){
		int a,b;
		cin>>a>>b;
		add(a,b);
		add(b,a);
	}
	sum(1,0);
//	for(int i=1;i<=n;i++)cout<<size[i]<<" ";
//	for(int i=1;i<=n;i++)cout<<f[i]<<" ";
//	cout<<endl;
	long long ans=0,q=-1e10,t=1;int flag=1;
	for(int i=2;i<=n;i++){
		if(size[i]>q){
			q=size[i];
			t=i;
		}
	}
	ans+=q;
	while(t!=0){
	//	cout<<1;
		cm[t]=1;
		t=f[t];
	}
	q=-1e10;
	for(int i=1;i<=n;i++){
	//	cout<<size[i]<<endl;
		if(cm[i]==0){
			q=max(q,size[i]);
			flag=0;
		}

	}
	if(flag)cout<<"Impossible";
	else	cout<<ans+q<<endl;
	return 0;
}


#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
typedef long long int64;
const int N = 2e5+5;
int n, w[N];
vector<int> edge[N];
int64 f[N], sum[N], ans;
void dfs(int x, int fa)
{
    sum[x] = w[x]; int64 d = 0;
    for(int i = 0; i < edge[x].size(); ++i)
    {
        int y = edge[x][i];
        if(y == fa) continue;
        dfs(y, x);
        d = max(d, f[x]+f[y]);
        sum[x] += sum[y];
        f[x] = max(f[x], f[y]);
    }
    f[x] = max(f[x], sum[x]);
    ans = max(ans, d);
}
int main()
{
    n = read();
    for(int x = 1; x <= n; ++x) w[x] = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    dfs(1, 0);
    if(!ans) puts("Impossible");
    else printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
#define inf 2147483647888888
#define ll long long
using namespace std;
const int Maxn=200866;

vector<ll> a[Maxn];
ll b[Maxn];
ll f[Maxn];
ll g[Maxn];
ll x,y,n,ans=-inf;

void dfs(ll fa,ll n){
    if(a[n].size()==1&&n!=1) {
        g[n]=b[n];
        return ;
    }
    ll sum=b[n];
    for(ll i=0;i<a[n].size();i++)  {
        ll pos=a[n][i];
        if(pos==fa)
        continue;
        dfs(n,pos);
        sum+=g[pos];
    }
    g[n]=sum;
}
void GetDeep(ll fa,ll n){
    if(a[n].size()==1&&n!=1)  {
        f[n]=g[n];
        return ;
    }
    f[n]=g[n];
    ll maxn1=-inf,maxn2=-inf,sum=0;
    for(ll i=0;i<a[n].size();i++){
        ll pos=a[n][i];
        if(pos==fa)
        continue;
        GetDeep(n,pos);
        if(f[pos]>maxn1)
        {
            maxn2=maxn1;
            maxn1=f[pos];
        }
        else
        maxn2=max(maxn2,f[pos]);
        f[n]=max(f[n],f[pos]);
    }
    ans=max(ans,maxn1+maxn2);
}
int main(){
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    scanf("%lld",&b[i]);
    for(ll i=0;i<n-1;i++){
        scanf("%lld%lld",&x,&y);
        a[y].push_back(x);
        a[x].push_back(y);
    }
    dfs(0,1);
    GetDeep(0,1);
    if(ans<-2147483647)
    {
        printf("Impossible");
        return 0;
    }
    printf("%lld",ans);
    return 0;
}


#include<bits/stdc++.h>
#pragma G++ optimize(2)
using namespace std;
const int N=200005;
int n,a[N],head[N],cnt,mx[2][N],ans,sum[N];
bool hv[2][N],ha;
struct edge{int to,nxt;}e[N<<1];
void Add(int a,int b){
	e[++cnt]=((edge){b,head[a]});
	head[a]=cnt;
}
void In(int x,int c){
	if(!hv[0][x]){
		hv[0][x]=1;
		mx[0][x]=c;
		return;
	}
	if(mx[0][x]<c){
		hv[1][x]=1;
		mx[1][x]=mx[0][x];
		mx[0][x]=c;
		return;
	}
	if(mx[1][x]<c || !hv[1][x]){
		mx[1][x]=c;
		hv[1][x]=1;
		return;
	}
}
void dfs(int x,int fa){
	sum[x]=a[x];
	for(int i=head[x];i;i=e[i].nxt){
		int y=e[i].to;
		if(y==fa)continue;
		dfs(y,x);
		sum[x]+=sum[y];
		In(x,mx[0][y]);
		In(x,mx[1][y]);
	}
	if(hv[0][x] && hv[1][x]){
		ans=max(ans,mx[0][x]+mx[1][x]);
		ha=1;
	}
	In(x,sum[x]);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1,a,b;i<n;i++){
		scanf("%d%d",&a,&b);
		Add(a,b);Add(b,a);
	}
	ans=-2e9;
	dfs(1,0);
	if(!ha)puts("Impossible");
	else printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 200010
typedef long long LL;
const LL INF = 1LL<<60;
struct node
{
    int v, nxt;
}edge[N*2];
LL dp[N], sum[N], w[N], head[N], tot, ans;

void add(int u, int v) {
    edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
}

void dfs(int u, int fa) {
    sum[u] = w[u];
    for(int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].v;
        if(v == fa) continue;
        dfs(v, u);
        sum[u] += sum[v];
        if(dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
        dp[u] = max(dp[u], dp[v]);
    }
    dp[u] = max(dp[u], sum[u]);
}

int main()
{
    int n;
    cin >> n;
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= n; i++) cin >> w[i];
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        add(u, v); add(v, u);
    }
    ans = -INF;
    for(int i = 1; i <= n; i++) dp[i] = -INF;
    dfs(1, -1);
    if(ans <= -INF) puts("Impossible");
    else cout << ans << endl;
    return 0;
}


def f(l):
    n  = len(l)
    c  = sum([i==l[i] for i in range(n)])
    d  = sum([l[l[i]]==i for i in range(n) if i!=l[i]])
    return c+2 if d>0 else c+1

q = int(input())
l = list(map(int,input().split()))
print(f(l))


def f(l):
    n  = len(l)
    c  = sum([i==l[i] for i in range(n)])
    if c==n:
        return c
    d  = sum([l[l[i]]==i for i in range(n) if i!=l[i]])
    return c+2 if d>0 else c+1

q = int(input())
l = list(map(int,input().split()))
print(f(l))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed = []
    single_swap = False
    double_swap = False

    for i in range(n):
        if permutation[i] == i:
            continue
        if not double_swap:
            for j in non_fixed:
                if permutation[i] == j and permutation[j] == i:
                    double_swap = True
                if not single_swap and (permutation[i] == j or permutation[j]) == i:
                    single_swap = True
        non_fixed.append(i)

    initial_nb_fixed = n - len(non_fixed)
    if double_swap:
        return initial_nb_fixed + 2
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed = []
    for i in range(n):
        if permutation[i] != i:
            non_fixed.append(i)
    initial_nb_fixed = n - len(non_fixed)
    single_swap = False
    for i in range(len(non_fixed)):
        p_i = non_fixed[i]
        for j in range(i + 1, len(non_fixed)):
            p_j = non_fixed[j]
            if permutation[p_i] == p_j and permutation[p_j] == p_i:
                return initial_nb_fixed + 2
            elif permutation[p_i] == p_j or permutation[p_j] == p_i:
                single_swap = True
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed_candidates = set()
    initial_nb_non_fixed = 0
    single_swap = False
    double_swap = False

    for i in range(n):
        if permutation[i] == i:
            continue
        if not double_swap:
            add_candidate = True
            for j in non_fixed_candidates:
                if permutation[i] == j and permutation[j] == i:
                    double_swap = True
                    break
                if permutation[i] == j or permutation[j] == i:
                    single_swap = True
                    non_fixed_candidates.remove(j)
                    add_candidate = False
                    break
            if add_candidate:
                non_fixed_candidates.add(i)
        initial_nb_non_fixed += 1

    initial_nb_fixed = n - initial_nb_non_fixed
    if double_swap:
        return initial_nb_fixed + 2
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed_candidates = set()
    initial_nb_non_fixed = 0
    single_swap = False
    double_swap = False

    for i in range(n):
        if permutation[i] == i:
            continue
        if not double_swap:
            add_candidate = True
            if non_fixed_candidates:
                candidates_removed = set()
                for j in non_fixed_candidates:
                    if permutation[i] == j and permutation[j] == i:
                        double_swap = True
                        add_candidate = False
                        break
                    if permutation[i] == j or permutation[j] == i:
                        single_swap = True
                        candidates_removed.add(j)
                        add_candidate = False
                non_fixed_candidates.difference_update(candidates_removed)
            if add_candidate:
                non_fixed_candidates.add(i)
        initial_nb_non_fixed += 1

    initial_nb_fixed = n - initial_nb_non_fixed
    if double_swap:
        return initial_nb_fixed + 2
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed = []
    single_swap = False
    double_swap = False

    for i in range(n):
        if permutation[i] == i:
            continue
        if not double_swap:
            for j in non_fixed:
                if permutation[i] == j and permutation[j] == i:
                    double_swap = True
                    break
                if not single_swap and (permutation[i] == j or permutation[j]) == i:
                    single_swap = True
        non_fixed.append(i)

    initial_nb_fixed = n - len(non_fixed)
    if double_swap:
        return initial_nb_fixed + 2
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    non_fixed_candidates = set()
    initial_nb_non_fixed = 0
    single_swap = False
    double_swap = False

    for i in range(n):
        if permutation[i] == i:
            continue
        if not double_swap:
            add_candidate = True
            candidates_removed = set()
            for j in non_fixed_candidates:
                if permutation[i] == j and permutation[j] == i:
                    double_swap = True
                    add_candidate = False
                    break
                if permutation[i] == j or permutation[j] == i:
                    single_swap = True
                    candidates_removed.add(j)
                    add_candidate = False
            non_fixed_candidates = non_fixed_candidates.difference(candidates_removed)
            if add_candidate:
                non_fixed_candidates.add(i)
        initial_nb_non_fixed += 1

    initial_nb_fixed = n - initial_nb_non_fixed
    if double_swap:
        return initial_nb_fixed + 2
    if single_swap:
        return initial_nb_fixed + 1
    return initial_nb_fixed

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    swap_candidates = set()
    init_nb_fixed_points = 0
    double_swap = False
    single_swap = False
    i = 0
    while not double_swap and i < n:
        if i == permutation[i]:
            init_nb_fixed_points += 1
            i += 1
            continue
        to_remove = set()
        for j in swap_candidates:
            if j == i:
                continue
            if i == permutation[j] and j == permutation[i]:
                double_swap = True
                break
            if i == permutation[j] or j == permutation[i]:
                single_swap = True
                to_remove.add(i)
                to_remove.add(j)
        swap_candidates.add(i)
        swap_candidates.difference_update(to_remove)
        i += 1

    if double_swap:
        return init_nb_fixed_points + 2
    if single_swap:
        return init_nb_fixed_points + 1
    return init_nb_fixed_points

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


from sys import stdin

def max_nb_fixed_points(n, permutation):
    pos_for_val = {}
    init_nb_fixed_points = 0
    double_swap = False
    single_swap = False
    for i in range(n):
        if i == permutation[i]:
            init_nb_fixed_points += 1
            continue
        if not double_swap and i in pos_for_val:
            j = pos_for_val[i]
            if permutation[i] == j:
                double_swap = True
            else:
                single_swap = True
        pos_for_val[permutation[i]] = i

    if double_swap:
        return init_nb_fixed_points + 2
    if single_swap:
        return init_nb_fixed_points + 1
    return init_nb_fixed_points

if __name__ == "__main__":
    n = int(stdin.readline())
    permutation = [int(c) for c in stdin.readline().split(" ")]
    print(max_nb_fixed_points(n, permutation))


def fixed_points(n, a):
    count = 0
    fixed = 0
    for i, j in enumerate(a):
        if i == j:
            fixed += 1
        elif a[j] == i:
            count = 2
        elif count == 0:
            count = 1

    return fixed + count

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    result = fixed_points(n, a)
    print(result)


# import math
# import bisect
import sys
from collections import Counter
input = sys.stdin.readline
def inp():
	return(int(input()))
def inlt():
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(s[:len(s)-1])
def invr():
	return(map(int,input().split()))
# list1, list2 = zip(*sorted(zip(list1, list2)))
N = inp()
L = inlt()
out = 0
fl  = True
for n in range(N):
	if n == L[n]:
		out += 1
	if fl and L[L[n]] == n:
		out += 2
		# print(n,L[n])
		fl = False
print(out)


# import math
# import bisect
import sys
from collections import Counter
input = sys.stdin.readline
def inp():
	return(int(input()))
def inlt():
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(s[:len(s)-1])
def invr():
	return(map(int,input().split()))
# list1, list2 = zip(*sorted(zip(list1, list2)))
N = inp()
L = inlt()
out = 0
fl  = True
for n in range(N):
	if n == L[n]:
		out += 1
	elif fl and L[L[n]] == n:
		out += 2
		# print(n,	L[n])
		fl = False
if fl and out < N:
	out += 1
print(out)


# import math
# import bisect
import sys
from collections import Counter
input = sys.stdin.readline
def inp():
	return(int(input()))
def inlt():
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(s[:len(s)-1])
def invr():
	return(map(int,input().split()))
# list1, list2 = zip(*sorted(zip(list1, list2)))
N = inp()
L = inlt()
out = 0
fl  = True
for n in range(N):
	if n == L[n]:
		out += 1
	if fl and L[L[n]] == n:
		out += 1
		fl = False
print(out)


import sys
sys.setrecursionlimit(100000)
n = int(input())
arr = [int(x) for x in sys.stdin.readline().strip().split()]
count=0
flag = False
for i in range(0,n):
	if arr[i]==i:
		count+= 1
	elif arr[arr[i]]==i:
		flag = True

if count!=n:
	print(count+1 if not flag else count+2)
else:
	print(count)


n = int(input())
ai = list(map(int,input().split()))

cnt = 0
for i in range(0,len(ai)):
    if ai[i] <= i:
        cnt += 1

print(cnt)


n = int(input())
ai = list(map(int,input().split()))

cnt = 0
cnt1 = 0
for i in range(0,len(ai)):
    if ai[i] == i:
        cnt1 += 1
        cnt += 1

#print(cnt)

if cnt != n:
    for i in range(n):
        if ai[i] != i:
            if ai[ai[i]] == i:
                cnt += 2
                break

    if cnt == cnt1:
        cnt += 1

print(cnt)


import sys
length = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
value_to_index = {}
index_to_value = {}
cnt = 0
temp = 0
final = 0
for index, x in enumerate(arr):
    value_to_index[x] = index
    index_to_value[index] = x

for index, x in enumerate(arr):
    if index == x:
        cnt += 1
    else:
        try:
            z = value_to_index[index]
            if z == x:
                temp = 2
            else:
                temp = 1
            final = max(final, temp)
        except KeyError:
            pass
print(cnt + final)


n=int(input())
arr=[int(i) for i in input().split()]
ans=0
boo=True
for i in range(n):
    if arr[i]!=i:
        if arr[arr[i]]==i:
            arr[arr[i]]=arr[i]
            arr[i]=i
            boo=False
            break
if boo:
    ans+=1
for i in range(n):
    if arr[i]==i:
        ans+=1
print(ans)


n=int(input())
arr=[int(i) for i in input().split()]
ans=0
boo=True
for i in range(n):
    if arr[i]!=i:
        if arr[arr[i]]==i:
            arr[arr[i]]=arr[i]
            arr[i]=i
            boo=False
            break
for i in range(n):
    if arr[i]==i:
        ans+=1
if ans!=n:
    if boo:
        ans+=1
print(ans)


from sys import stdin
n=int(input())
l=list(map(int,stdin.readline().split()))
i=0
dict={}
counter=0
a=[int(i) for i in range(n)]
if(a==l):
    print(n)
else:
    for e in l:
        if(e==i):
            counter+=1
        else:
            dict[e]=i
        i+=1
    for e,f in dict.items():
        if(dict.get(f)==e):
            print(counter+2)
            break
    else:
        print(counter+1)


from sys import stdin
n=int(input())
l=list(map(int,stdin.readline().split()))
i=0
dict={}
counter=0
for e in l:
    if(e==i):
        counter+=1
    else:
        dict[e]=i
    i+=1
for e,f in dict.items():
    if(dict.get(f)==e):
        print(counter+2)
        break
else:
    print(counter+1)


n = int(input())
a = [int(i) for i in input().split()]

count = 0
add = 0
for i in range(n):
    if (a[i] == i):
        count += 1
    else:
        if (a[a[i]] == i):
            add = 1

if (count != n):
    add += 1

print (count + add)


n = int(input())

a = list(map(int, input().split()))
c = 0
t = 0

for i in range(n):
 if a[i] == i:
  c+=1
 elif i == a[a[i]]:
  t=1
print(c+t+int(c!=n))


from math import *
from collections import *
def inp(): return map(int,input().split())
def inp_arr(): return list(map(int,input().split()))
def lcm(a,b):return (a*b)/gcd(a,b)

n=int(input())
arr = inp_arr()
c=0
flag=0
for i in range(len(arr)):
    if i==arr[i]:c+=1
    elif arr[arr[i]]==i and flag==0:
        c+=2
        flag=1
print(c+1 if flag==0 else c)


from math import *
from collections import *
def inp(): return map(int,input().split())
def inp_arr(): return list(map(int,input().split()))
def lcm(a,b):return (a*b)/gcd(a,b)

n=int(input())
arr = inp_arr()
c=0
flag=0
for i in range(len(arr)):
    if i==arr[i]:
        c+=1
        if i==len(arr)-1 and flag==0:flag=1
    elif arr[arr[i]]==i and flag==0:
        c+=2
        flag=1
print(c+1 if flag==0 else c)


from math import *
from collections import *
def inp(): return map(int,input().split())
def inp_arr(): return list(map(int,input().split()))
def lcm(a,b):return (a*b)/gcd(a,b)

n=int(input())
arr = inp_arr()
c=0
for i in range(len(arr)):
    if i==arr[i]:c+=1
print(c+1)


def permutations(n,arr):
    count = 0
    flag = False
    for i in range(n):
        if i == arr[i]:
            count += 1
        else:
            if i == arr[arr[i]]:
                flag = True
    if count == n:
        print(count)
        return
    elif flag:
        count += 2
    else:
        count += 1
    print(count)

n = int(input())
l = list(map(int,input().split()))
permutations(n,l)


def permutations(n,arr):
    count = 0
    flag = False
    for i in range(n):
        if i == arr[i]:
            count += 1
        else:
            if i == arr[arr[i]]:
                flag = True
    if flag:
        count += 2
    else:
        count += 1
    print(count)

n = int(input())
l = list(map(int,input().split()))
permutations(n,l)


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x;
ll fun(ll arr[n],ll brr[n]){
ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(j<n){
if(arr[j]<=f&&arr[j]>=1){
swing++;
}}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
if(f<(n-1)) {f++;if(x>=f)i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=f+1)i=x-f;
else i=-1;}}

}
return 0;
}
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;
if(arr[i]==1)  x=i;}
brr[0]=1;
brr[n-1]=1;
fun(arr,brr);
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x,p,y;
/*ll fun(ll arr[n],ll brr[n],ll crr[n]){

return 0;
}*/
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],crr[n+2];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
//brr[i]=0;
crr[arr[i]]=i;}
crr[0]=1e6;
crr[n+1]=1e6;
//brr[0]=1;
//brr[n-1]=1;
//fun(arr,brr,crr);
ll count=1;
printf("1");
for(int i=2;i<=n;i++){count =1;
x=crr[1];
while( (arr[x+1] <= arr[crr[i]]) && (x<=(n-1) ) ){
x++;
count++;
}
y=crr[1];
while( (arr[y-1] <= arr[crr[i]]) && ( y>=1) ){
y--;
count++;
}
if(count==i) printf("1");
else printf("0");
}
//for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}

/*ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(j<n){
if(arr[j]<=f&&arr[j]>=1){
swing++;
if(arr[j]==f) p=j;
}}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
x=p;
if(f<(n-1)) {f++;if(x>=(f+1))i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=(f+1))i=x-f;
else i=-1;}}

}*/


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x,p;
ll fun(ll arr[n],ll brr[n]){
ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(j<n){
if(arr[j]<=f&&arr[j]>=1){
swing++;
if(arr[j]==f) p=j;
}}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
x=p;
if(f<(n-1)) {f++;if(x>=f+1)i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=f+1)i=x-f;
else i=-1;}}

}
return 0;
}
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;
if(arr[i]==1)  x=i;}
brr[0]=1;
brr[n-1]=1;
fun(arr,brr);
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x;
ll fun(ll arr[n],ll brr[n]){
ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(arr[j]<=f&&arr[j]>=1){
swing++;
}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
if(f<(n-1)) {f++;if(x>=f)i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=f)i=x-f;
else i=-1;}}

}
return 0;
}
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;
if(arr[i]==1)  x=i;}
brr[0]=1;
brr[n-1]=1;
fun(arr,brr);
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x;
ll fun(ll arr[n],ll brr[n]){
ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(arr[j]<=f&&arr[j]>=1){
swing++;
}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
if(f<(n-1)) {f++;if(x>=f)i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=f+1)i=x-f;
else i=-1;}}

}
return 0;
}
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;
if(arr[i]==1)  x=i;}
brr[0]=1;
brr[n-1]=1;
fun(arr,brr);
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}


#include<stdio.h>
#include<string.h>
#define ll long long
ll n,x,p;
/*ll fun(ll arr[n],ll brr[n],ll crr[n]){

return 0;
}*/
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n],crr[n+2];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;
crr[arr[i]]=i;}
crr[0]=1e6;
crr[n+1]=1e6;
brr[0]=1;
brr[n-1]=1;
//fun(arr,brr,crr);
ll count=1;
for(int i=2;i<n;i++){count =1;
x=crr[1];
while( (arr[x+1] <= arr[crr[i]]) && (x<=(n-1) ) ){
x++;
count++;
}
x=crr[1];
while( (arr[x-1] <= arr[crr[i]]) && ( x>=1) ){
x--;
count++;
}
if(count==i) brr[i-1]=1;
else brr[i-1]=0;
}
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}

/*ll f=2,swing=0,l=0;
for(int i=x-f+1;i<=x;i++){swing=0;
for(int j=i;j<i+f;j++){
if(j<n){
if(arr[j]<=f&&arr[j]>=1){
swing++;
if(arr[j]==f) p=j;
}}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
x=p;
if(f<(n-1)) {f++;if(x>=(f+1))i=x-f;
else i=-1;}
}
if(i==(x)){
if(f<(n-1)) {f++;
if(x>=(f+1))i=x-f;
else i=-1;}}

}*/


#include<stdio.h>
#include<string.h>
#define ll long long
ll n;
ll fun(ll arr[n],ll brr[n]){
ll f=2,swing=0,l=0;
for(int i=0;i<n-f+1;i++){swing=0;
for(int j=i;j<i+f;j++){
if(arr[j]<=f&&arr[j]>=1){
swing++;
}
}//printf("%d %lld\n",i,swing);
if(swing==f){ brr[f-1]=1;
if(f<(n-1)) {f++;i=-1;}
}
if(i==(n-f)){
if(f<(n-1)) {f++;i=-1;}}

}
return 0;
}
int main(){ll t;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
ll arr[n],brr[n];
for(int i=0;i<n;i++) {scanf("%lld",&arr[i]);
brr[i]=0;}
brr[0]=1;
brr[n-1]=1;
fun(arr,brr);
for(int i=0;i<n;i++) printf("%lld",brr[i]);
printf("\n");
}
return 0;}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,m,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        for(i=1;i<n;i++){
            b[a[i]]=1;
            if(a[i]<m) m=a[i];
            for(k=m;k<m+i;k++)
              if(b[k]==0) break;
            if(k==m+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,m,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        for(i=1;i<n;i++){
            b[a[i]]=1;
            if(a[i]<m) m=a[i];
            else{for(k=m;k<m+i;k++)
                  if(b[k]==0) break;}
            if(k==m+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,m,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        for(i=1;i<n;i++){
            b[a[i]]=1;
            if(a[i]<m) m=a[i];
            if(a[i]-m>i-1) k=-1;
            else{for(k=m;k<m+i;k++)
                  if(b[k]==0) break;}
            if(k==m+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,m1,m2,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        m1=n;
        m2=0;
        for(i=1;i<n;i++){
            b[a[i]]=1;
            if(a[i]<m1) m1=a[i];
            if(a[i]>m2) m2=a[i];
            if(m2-m1==i-1) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,len,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        for(i=1;i<n;i++){
            b[a[i]]=1;
            for(j=0;j<n;j++)
              if(b[j]==1) break;
            for(k=j;k<j+i;k++)
                if(b[k]==0) break;
            if(k==j+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,m,k,a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        m=n;
        for(i=1;i<n;i++){
            b[a[i]]=1;
            if(a[i]<m) m=a[i];
            for(k=m;k<m+i;k++)
              if(b[k]==0) break;
            if(k==m+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
     int t,i,j,n,len,k;
     char a[200002];
     scanf("%d",&t);
     while(t--){
        int b[200002]={0};
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%d",&j);
            a[j]=i;
        }
        for(i=1;i<n;i++){
            b[a[i]]=1;
            for(j=0;j<n;j++)
              if(b[j]==1) break;
            for(k=j;k<j+i;k++)
                if(b[k]==0) break;
            if(k==j+i) printf("1");
            else printf("0");
        }
        printf("1\n");
     }
     return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	int n,i,t,j;
	int position,left_position,right_position;
	int total,max;
	int *arr;
	scanf("%d",&t);
	for(j=1;j<=t;j++){
	    scanf("%d",&n);
	    arr=(int*)malloc(n*sizeof(int));
	    position=-1,total=0;
	    for(i=0;i<n;i++){
		    scanf("%d",(arr+i));
		    if(*(arr+i)==1)
		    position=i;
	    }
	    left_position=right_position=position;
	    while(total!=n)
	    {
	    	if(total==0){
	    		total++;
	    		max=1;
	    		printf("%d",1);
			}
	    	else{
	    		total++;
	    		if(right_position<n&&left_position>=0)
	    		{
	    			if(arr[right_position+1]<arr[left_position-1]){
	    				right_position++;
	    				max=arr[right_position]>max?arr[right_position]:max;
	    				max==total?printf("%d",1):printf("%d",0);
					}
	    			else{
	    				left_position--;
	    				max=arr[left_position]>max?arr[left_position]:max;
	    				max==total?printf("%d",1):printf("%d",0);
					}
				}
				else if(right_position==n)
				{
					left_position--;
					max=arr[left_position]>max?arr[left_position]:max;
					if(left_position>=0)
					max==total?printf("%d",1):printf("%d",0);
				}
				else if(left_position==-1){
					right_position++;
					max=arr[right_position]>max?arr[right_position]:max;
					max==total?printf("%d",1):printf("%d",0);
				}
			}
		}
		printf("\n");
	    free(arr);
    }
}


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
	int n,i,t,j;
	int position,left_position,right_position;
	int total,max;
	int *arr;
	scanf("%d",&t);
	for(j=1;j<=t;j++){
	    scanf("%d",&n);
	    arr=(int*)malloc(n*sizeof(int));
	    position=-1,total=0;
	    for(i=0;i<n;i++){
		    scanf("%d",(arr+i));
		    if(*(arr+i)==1)
		    position=i;
	    }
	    left_position=right_position=position;
	    while(total!=n)
	    {
	    	if(total==0){
	    		total++;
	    		max=1;
	    		printf("%d",1);
			}
	    	else{
	    		total++;
	    		if((right_position+1)<n&&(left_position-1)>=0)
	    		{
	    			if(arr[right_position+1]<arr[left_position-1]){
	    				right_position++;
	    				max=arr[right_position]>max?arr[right_position]:max;
	    				max==total?printf("%d",1):printf("%d",0);
					}
	    			else{
	    				left_position--;
	    				max=arr[left_position]>max?arr[left_position]:max;
	    				max==total?printf("%d",1):printf("%d",0);
					}
				}
				else if(right_position==(n-1)&&left_position>0)
				{
					left_position--;
					max=arr[left_position]>max?arr[left_position]:max;
					max==total?printf("%d",1):printf("%d",0);
				}
				else if(left_position==0){
					right_position++;
					max=arr[right_position]>max?arr[right_position]:max;
					max==total?printf("%d",1):printf("%d",0);
				}
			}
		}
		printf("\n");
	    free(arr);
    }
}


#include<stdio.h>
#include<limits.h>

long long int arr[2100000];

int main ()
{
	long long int test, n, t, rsum=0,sum, min;

	scanf("%lld",&test);

	while (test--)
	{
		rsum=0;

		min=INT_MAX;

		scanf("%lld",&n);

		char str[n];

		for (int i=0;i<n;i++)
		{
			scanf("%lld",&t);
			arr[t]=i+1;
		}

		for (int i=1;i<=n;i++)
		{
			rsum+=arr[i];

			if (min>arr[i])
				min=arr[i];

			sum = (i*(2*min+i-1))/2;

//			printf("%lld %lld\n", rsum, sum);

			if (rsum==sum)
				str[i-1]='1';

			else
				str[i-1]='0';

			printf("%c",str[i-1]);
		}

		printf("\n");
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 222222
int min(int a, int b) {return a<b?a:b;}
int max(int a, int b) {return a>b?a:b;}
int tc, n, inp[N], tmp, l, r;
int main()
{
    scanf("%d", &tc);
    while (tc--)
    {
        scanf("%d", &n);
        for (int i=1; i<=n; i++) scanf("%d", &tmp), inp[tmp]=i;
        l=n+1;
        r=0;
        for (int i=1; i<=n; i++)
        {
            l=min(l, inp[i]);
            r=max(r, inp[i]);
            printf("%d", (r-l+1==i)?1:0);
        }
        printf("\n");
    }
}


#include<stdio.h>
int main(void)
{
    int i,j,k,t,l,x,z1,z2,m,n,p1,p2,max,count;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        int array[n],extra[n];
        char string[n+3];
        string[0]='1';
        string[n-1]='1';
        count=2;
        for(j=0,m=0;j<n;j++)
        {
            scanf("%d",&array[j]);
            extra[j]=j;
            if(array[j]==1)
            {
                m=1;
                x=j;
                if(j-1>=0)
                    k=j-1;
                else
                    k=-1;
                if(j+1<n)
                    l=j+1;
                else
                    l=-1;
            }
            else if(m&&j>x)
            {
                if(!extra[count-2])
                    extra[count-1]-=(count-2);
                else
                    extra[count-1]-=(count-2-extra[count-2]);
                for(z1=0,z2=0;;)
                {
                    if(array[k]<=count&&!z1&&k>=0)
                    {
                        extra[count-1]--;
                        array[k]=n+1;
                        if(k-1>=0)
                            k--;
                    }
                    else
                        z1=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                    if(array[l]<=count&&!z2&&l<n&&l>=0)
                    {
                        extra[count-1]--;
                        array[l]=n+1;
                        if(l+1<n)
                            l++;
                    }
                    else
                        z2=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                }
                if(!extra[count-1])
                    string[count-1]='1';
                else
                    string[count-1]='0';
                count++;
            }
        }
        while(count<n)
        {
            if(!extra[count-2])
                extra[count-1]-=(count-2);
            else
                extra[count-1]-=(count-2-extra[count-2]);
            for(z1=0,z2=0;;)
            {
                if(array[k]<=count&&!z1&&k>=0)
                {
                    extra[count-1]--;
                    array[k]=n+1;
                    if(k-1>=0)
                        k--;
                }
                else
                    z1=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
                if(array[l]<=count&&!z2&&l<n&&l>=0)
                {
                    extra[count-1]--;
                    array[l]=n+1;
                    if(l+1<n)
                        l++;
                }
                else
                    z2=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
            }
            if(!extra[count-1])
                string[count-1]='1';
            else
                string[count-1]='0';
            count++;
        }
        string[n]='\0';
        printf("%s\n",string);
    }
}


#include<stdio.h>
int main(void)
{
    int i,j,k,t,l,x,z1,z2,last,m,n,max,count;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        int array[n],extra[n];
        char string[n+3];
        string[0]='1';
        string[n-1]='1';
        count=2;
        for(j=0,m=0,last=0;j<n;j++)
        {
            scanf("%d",&array[j]);
            extra[j]=j;
            if(array[j]==1)
            {
                m=1;
                x=j;
                if(j-1>=0)
                    k=j-1;
                else
                    k=-1;
                if(j+1<n)
                    l=j+1;
                else
                    l=-1;
            }
            else if(m&&j>x)
            {
                extra[count-1]-=last;
                for(z1=0,z2=0;;)
                {
                    if(array[k]<=count&&!z1)
                    {
                        extra[count-1]--;
                        if(k-1>=0)
                            k--;
                    }
                    else
                        z1=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                    if(array[l]<=count&&!z2)
                    {
                        extra[count-1]--;
                        if(l+1<n)
                            l++;
                    }
                    else
                        z2=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                }
                if(!extra[count-1])
                {
                    string[count-1]='1';
                    last=count-1;
                }
                else
                    string[count-1]='0';
                count++;
            }
        }
        while(count<n)
        {
            extra[count-1]-=last;
            for(z1=0,z2=0;;)
            {
                if(array[k]<=count&&!z1)
                {
                    extra[count-1]--;
                    if(k-1>=0)
                        k--;
                }
                else
                z1=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
                if(array[l]<=count&&!z2)
                {
                    extra[count-1]--;
                    if(l+1<n)
                        l++;
                }
                else
                    z2=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
            }
            if(!extra[count-1])
            {
                string[count-1]='1';
                last=count-1;
            }
            else
                string[count-1]='0';
            count++;
        }
        string[n]='\0';
        printf("%s\n",string);
    }
}


#include<stdio.h>
int main(void)
{
    int i,j,k,t,l,x,z1,z2,last,m,n,p1,p2,max,count;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        int array[n],extra[n];
        char string[n+3];
        string[0]='1';
        string[n-1]='1';
        count=2;
        for(j=0,m=0,last=0;j<n;j++)
        {
            scanf("%d",&array[j]);
            extra[j]=j;
            if(array[j]==1)
            {
                m=1;
                x=j;
                if(j-1>=0)
                    k=j-1;
                else
                    k=-1;
                if(j+1<n)
                    l=j+1;
                else
                    l=-1;
            }
            else if(m&&j>x)
            {
                if(!extra[count-2])
                    extra[count-1]-=(count-2);
                else
                    extra[count-1]-=(count-2-extra[count-2]);
                for(z1=0,z2=0;;)
                {
                    if(array[k]<=count&&!z1)
                    {
                        extra[count-1]--;
                        array[k]=n+1;
                        if(k-1>=0)
                            k--;
                    }
                    else
                        z1=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                    if(array[l]<=count&&!z2)
                    {
                        extra[count-1]--;
                        array[l]=n+1;
                        if(l+1<n)
                            l++;
                    }
                    else
                        z2=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                }
                if(!extra[count-1])
                {
                    string[count-1]='1';
                    last=count-1;
                }
                else
                    string[count-1]='0';
                count++;
            }
        }
        while(count<n)
        {
            if(!extra[count-2])
                extra[count-1]-=(count-2);
            else
                extra[count-1]-=(count-2-extra[count-2]);
            for(z1=0,z2=0;;)
            {
                if(array[k]<=count&&!z1)
                {
                    extra[count-1]--;
                    array[k]=n+1;
                    if(k-1>=0)
                        k--;
                }
                else
                    z1=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
                if(array[l]<=count&&!z2)
                {
                    extra[count-1]--;
                    array[l]=n+1;
                    if(l+1<n)
                        l++;
                }
                else
                    z2=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
            }
            if(!extra[count-1])
                string[count-1]='1';
            else
                string[count-1]='0';
            count++;
        }
        string[n]='\0';
        printf("%s\n",string);
    }
}


#include<stdio.h>
int main(void)
{
    int i,j,k,t,l,x,z1,z2,m,n,p1,p2,max,count;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        int array[n],extra[n];
        char string[n+3];
        string[0]='1';
        string[n-1]='1';
        count=2;
        for(j=0,m=0;j<n;j++)
        {
            scanf("%d",&array[j]);
            extra[j]=j;
            if(array[j]==1)
            {
                m=1;
                x=j;
                if(j-1>=0)
                    k=j-1;
                else
                    k=-1;
                if(j+1<n)
                    l=j+1;
                else
                    l=-1;
            }
            else if(m&&j>x)
            {
                if(!extra[count-2])
                    extra[count-1]-=(count-2);
                else
                    extra[count-1]-=(count-2-extra[count-2]);
                for(z1=0,z2=0;;)
                {
                    if(array[k]<=count&&!z1&&k>=0)
                    {
                        extra[count-1]--;
                        array[k]=n+1;
                        if(k-1>=0)
                            k--;
                    }
                    else
                        z1=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                    if(array[l]<=count&&!z2&&l<n)
                    {
                        extra[count-1]--;
                        array[l]=n+1;
                        if(l+1<n)
                            l++;
                    }
                    else
                        z2=1;
                    if(z1&&z2)
                        break;
                    if(!extra[count-1])
                        break;
                }
                if(!extra[count-1])
                    string[count-1]='1';
                else
                    string[count-1]='0';
                count++;
            }
        }
        while(count<n)
        {
            if(!extra[count-2])
                extra[count-1]-=(count-2);
            else
                extra[count-1]-=(count-2-extra[count-2]);
            for(z1=0,z2=0;;)
            {
                if(array[k]<=count&&!z1&&k>=0)
                {
                    extra[count-1]--;
                    array[k]=n+1;
                    if(k-1>=0)
                        k--;
                }
                else
                    z1=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
                if(array[l]<=count&&!z2&&l<n)
                {
                    extra[count-1]--;
                    array[l]=n+1;
                    if(l+1<n)
                        l++;
                }
                else
                    z2=1;
                if(z1&&z2)
                    break;
                if(!extra[count-1])
                    break;
            }
            if(!extra[count-1])
                string[count-1]='1';
            else
                string[count-1]='0';
            count++;
        }
        string[n]='\0';
        printf("%s\n",string);
    }
}