/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define MOD 1000000007
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, m, a[N];
vector<int> g[N], r[N];
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;
g[u].pb(v);
g[v].pb(u);
}
vector<int> d1(n+1, MOD);
vector<int> dn(n+1, MOD);
vector<int> m1(n+1, 0);
vector<int> mn(n+1, 0);
priority_queue<pair<int, int>> q;
q.push({0, 1});
d1[1] = 0;
m1[1] = 0;
vector<bool> vis(n+1);
while(!q.empty()){
int v = q.top().second;
q.pop();
if(vis[v]) continue;
for(int u: g[v]){
int w = max(1, a[u] - a[v]);
if(d1[v] + w < d1[u]){
m1[u] = max(a[u], m1[v]);
d1[u] = d1[v] + w;
q.push({-d1[u], u});
}
}
}
q.push({0, n});
fill(all(vis), 0);
dn[n] = 0;
mn[n] = 0;
while(!q.empty()){
int v = q.top().second;
q.pop();
if(vis[v]) continue;
for(int u: g[v]){
int w = max(1, a[u] - a[v]);
if(dn[v] + w < dn[u]){
mn[u] = max(a[u], mn[v]);
dn[u] = dn[v] + w;
q.push({-dn[u], u});
}
}
}
// int ans = MOD;
// for(int i = 1; i <= n; ++i) if(m1[i] == a[i] && mn[i] == a[i]) ans = min(ans, d1[i] + dn[i]);
// cout << ans;
vector<int> dp(n+1, 0);
dp[1] = 0;
for(int i = 2; i <= n; ++i){
dp[i] = dp[i - 1] + max(1, a[i] - a[i - 1]);
}
vector<int> dp2(n+1, 0);
dp2[1] = 0;
for(int i = n-1; i >= 1; --i){
dp2[i] = dp2[i + 1] + max(1, a[i] - a[i+1]);
}
int ans = MOD;
int mx = *max_element(a+1,a+1+n);
for(int i = 1; i <= n; ++i){
if(a[i] == mx) ans = min(ans, dp[i] + dp2[i]);
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
solve();
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
47704 KB |
Output is correct |
2 |
Incorrect |
69 ms |
60816 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
47704 KB |
Output is correct |
2 |
Incorrect |
11 ms |
47768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
47704 KB |
Output is correct |
2 |
Incorrect |
11 ms |
47768 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
47704 KB |
Output is correct |
2 |
Incorrect |
69 ms |
60816 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |