#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vi vector<long long>
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pr pair<ll, ll>
#define ff first
#define ss second
void solution();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
#define arr array<ll, 4>
const int N = 2e5+10;
vi adj[N];
ll a[N];
void solution() {
ll n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
while (m--) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vi ng(n+1, -1);
stack<ll> hs;
for (int i = n; i >= 1; i--) {
while (hs.size() > 0 && a[hs.top()] < a[i]) hs.pop();
if (hs.size()) ng[i] = hs.top();
hs.push(i);
}
ll ans = 0;
ll x = 0;
for (int i = 1; i < n;) {
if (ng[i] == -1) {
x--;
ans++;
i++;
} else {
ll delta = a[ng[i]] - x;
x += delta;
ans += max(delta, ng[i]-i);
i = ng[i];
}
}
cout << ans + x << endl;
}
| # | 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... |