#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main(){


 //input
 int N;
 cin >> N;

 string name[N];
 int height[N];
 vector<int> height_checker(N);

 for(int i = 0; i < N; i++){
   cin >> name[i] >> height[i];
   height_checker.at(i) = height[i];
 }


 //高さをsort
 sort(height_checker.begin(), height_checker.end());

 //N-1番目の山の高さを聞き、それと合う山を探す。
 int SecondMountain = height_checker.at(N-2);
  for(int j = 0; j < N; j++){
   if(height[j] == SecondMountain){cout << name[j] << endl; break;}
  }



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

#define fastio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output            freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 

template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }

template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }

template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }

#define int                     long long
#define all(ds)                 ds.begin(), ds.end()
#define uniq(v)                 (v).erase(unique(all(v)),(v).end())
#define gcd(x,y)                __gcd(x,y)
#define rep(i,a,b)              for(int i=a;i<b;i++)
#define precise(x)              cout<<fixed<<setprecision(x)
#define endl                    "\n"

const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 2e5+5;

int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
// int ans = 0;
vector<int>edges[N];
vector<int>mn(N, INF); // min cost of gold among all the cities which lead to the current node

void solve(){
    read(n,m);
    rep(i,1,n+1)read(a[i]);
    rep(i,0,m){
        read(x,y);
        edges[x].push_back(y);
    }
    int ans = -INF;
    for(int i = 1; i <= n; ++i){
        for(int j: edges[i]){
            mn[j] = min(mn[j], min(mn[i], a[i]));
        }
        if(mn[i] != INF){
            ans = max(ans, a[i]-mn[i]);
        }
    }
    prn(ans);
}

signed main()
{
#ifndef ONLINE_JUDGE  
    input_output  
#else   
    fastio
#endif

#ifdef SIEVE
    sieve();
#endif

#ifdef NCR
    init();
#endif

    int t = 1;
    // cin>>t;
    while(t--){
        solve();
    }

    return 0;
}