#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 2e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
int n, m;
int a[maxN];
vector <int> adj[maxN];
vector <pii> all;
namespace sub1 {
int checker = 0;
void sol () {
int mx = 0, pos = 1;
for (int i = 1; i <= n; i ++) {
if (mx <= a[i]) {
mx = a[i];
pos = i;
}
}
int curH = 0, time = 0;
for (int i = 2; i <= pos; i ++) {
if (curH == mx) {
time ++;
}
else {
time += max (a[i] - curH, 1LL);
curH = max (curH + 1, a[i]);
}
}
curH = 0;
for (int i = n - 1; i >= pos; i --) {
if (curH == mx) {
time ++;
}
else {
time += max (a[i] - curH, 1LL);
curH = max (curH + 1, a[i]);
}
}
cout << time;
return;
}
}
namespace sub2 {
const int N2 = 2e3 + 5;
struct Edge {
int node, hi;
};
int L[N2][N2];
int limH = 0;
void bfs () {
for (int i = 0; i <= n; i ++) {
for (int j = 0; j <= limH; j ++) {
L[i][j] = inf;
}
}
queue <Edge> q;
L[1][0]= 0;
q.push ({1, 0});
while (!q.empty ()) {
auto [u, h] = q.front ();
q.pop ();
//cout << u << ' ' << h << ' ' << L[u][h] << '\n';
for (auto v : adj[u]) {
for (int nh = max (h - 1, 0LL); nh <= min (limH, h + 1); nh ++) {
if (L[v][nh] == inf && nh >= a[v]) {
L[v][nh] = L[u][h] + 1;
q.push ({v, nh});
}
if (L[u][nh] == inf && nh >= a[u]) {
L[u][nh] = L[u][h] + 1;
q.push ({u, nh});
}
}
}
}
}
void sol () {
limH = *max_element (a + 1, a + n + 1);
bfs ();
int res = 0;
cout << L[n][0];
}
}
namespace subfull {
int L1[maxN], Ln[maxN];
void dijkstra () {
for (int i = 1; i <= n; i ++) {
L1[i] = Ln[i] = inf;
}
priority_queue <pii, vector <pii>, greater <pii>> pq;
L1[1] = 0;
pq.push ({L1[1], 1});
while (!pq.empty ()) {
auto [c, u] = pq.top ();
pq.pop ();
if (c > L1[u]) {
continue;
}
for (auto v : adj[u]) {
if (L1[v] > max (L1[u] + 1, a[v])) {
L1[v] = max (L1[u] + 1, a[v]);
pq.push ({L1[v], v});
}
}
}
Ln[n] = 0;
pq.push ({Ln[n], n});
while (!pq.empty ()) {
auto [c, u] = pq.top ();
pq.pop ();
if (c > Ln[u]) {
continue;
}
for (auto v : adj[u]) {
if (Ln[v] > max (Ln[u] + 1, a[v])) {
Ln[v] = max (Ln[u] + 1, a[v]);
pq.push ({Ln[v], v});
}
}
}
}
void sol () {
dijkstra ();
int res = inf;
for (int i = 1; i <= n; i ++) {
mini (res, 2 * max (L1[i], Ln[i]));
}
for (auto [u, v] : all) {
mini (res, 2 * min (max (L1[u], Ln[v]), max (L1[v], Ln[u])) + 1);
}
cout << res;
return;
}
};
void solve () {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
for (int i = 1; i <= m; i ++) {
int u, v;
cin >> u >> v;
adj[u].push_back (v);
adj[v].push_back (u);
all.push_back ({u, v});
if (u == i && v == i + 1) {
sub1 :: checker ++;
}
}
if (sub1 :: checker == n - 1) {
sub1 :: sol ();
}
else if (n <= 2000 && *max_element (a + 1, a + n + 1) <= 2000) {
sub2 :: sol ();
}
else {
subfull :: sol ();
}
return;
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:192:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:193:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |