#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define int long long
#define ll long long
#define INF 1LL << 33
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(p) \
    if (test)   \
        cout << #p " : " << p << endl;
//bool test = true;
bool test = false;
template <class T>
[[maybe_unused]] bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
int N, max_A, max_B, tmp, ans;
vector<pair<int, int>> A, B;
bool same;
signed main()
{
    cin >> N;
    A.resize(N);
    B.resize(N);
    REP(i, N)
    {
        cin >> tmp;
        A.at(i) = {tmp, i};
        cin >> tmp;
        B.at(i) = {tmp, i};
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    max_A = A.at(0).first;
    max_B = B.at(0).first;
    ans = max_A + max_B;
    REP(i, N)
    {
        if (ans < A.at(i).first)
            break;
        REP(j, N)
        {
            if (test)
                cout << "i: " << i << " j: " << j << endl;
            if (ans < B.at(i).first)
                break;
            if (A.at(i).second != B.at(j).second) {
                same = false;
                chmin(ans, max(A.at(i).first, B.at(j).first));
            } else {
                same = true;
                chmin(ans, A.at(i).first + B.at(j).first);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    ll N;
    cin >> N;
    vl C(N);
    rep(i, N) cin >> C[i], C[i]--;
    vvc<ll> G(N);
    rep(i, N - 1){
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].eb(b), G[b].eb(a);
    }

    vl ok(N), U(100000, -1);
    auto dfs = [&](auto&& dfs, ll now, ll par) -> void {
        if(U[C[now]] == -1) {
            ok[now] = 1;
            U[C[now]] = now;
        }
        repa(to, G[now]){
            if (to == par) continue;
            dfs(dfs, to, now);
        }
        if(U[C[now]] == now){
            U[C[now]] = -1;
        }
    };

    dfs(dfs, 0, -1);
    rep(i, N){
        if(ok[i]){
            cout << i + 1 << '\n';
        }
    }
}