#6一直WA。。。 QWQ
#include <bits/stdc++.h>
using namespace std;
struct NODE{
vector<int> to;
int in;
}arr[500005];
long long ans = 1e18;
int a[500005], dp[500005]; //自己画图举个例子,就知道dp什么用处了。
queue<int> que;
int main()
{
int n, m, num = 0;
// long long maxx1 = 0, maxx2 = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
dp[i] = a[i];
// (a[i] >= maxx1) ? maxx2 = maxx1, maxx1 = a[i] : maxx1 = maxx1;
}
//ans = maxx1 + maxx2 + 5;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
arr[u].to.push_back(v);
arr[v].in++;
}
for (int i = 1; i <= n; i++)
if (!arr[i].in) que.push(i);
while (!que.empty())
{
// num++;
int now = que.front();
que.pop();
for (int i : arr[now].to)
{
arr[i].in--;
ans = min<long long>(ans, dp[now] + a[i]);
dp[i] = min(dp[now], dp[i]);
if (!arr[i].in) que.push(i);
}
}
for (int i = 1; i <= n; i++) //本来就有环。
if (arr[i].in) ans = 0;
cout << ans << endl;
// if (num == n) cout << ans << endl;
// else cout << 0 << endl;
return 0;
}