#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define int long long
int a[200005], f[200005], d[200005];
vector<int> g[200005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
f[i] = d[i] = -1;
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
priority_queue<pair<int, int>> q;
q.push({0, 1});
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int x = -p.first, u = p.second;
for (int w : g[u]) {
if (f[w] != -1) continue;
f[w] = max(x + 1, a[w]);
q.push({-f[w], w});
}
}
q.push({0, n});
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int x = -p.first, u = p.second;
for (int w : g[u]) {
if (d[w] != -1) continue;
d[w] = max(x + 1, a[w]);
q.push({-d[w], w});
}
}
int res = 1e9;
for (int i = 1; i <= n; i++) {
res = min(res, max(f[i], d[i]) * 2);
}
for (int i = 1; i <= n; i++) {
for (int w : g[i]) {
if (f[i] == d[w]) res = min(res, f[i] * 2 + 1);
}
}
cout << res;
}
Compilation message
cc1plus: error: '::main' must return 'int'