#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define sz(a) (ll) a.size()
#define all(x) (x).begin(), (x).end()
//[NOISG 2023 Finals] Airplane
ll n, m;
vector<ll> a;
void f() {
cin >> n >> m;
a.resize(n);
for(auto& u : a) cin >> u;
ll mxa=*max_element(all(a));
ll dummy;
for(ll i=0; i<(n-1)*2; i++) cin >> dummy;
if(n==2){
cout << 0;
return;
}
ll l=-1, r=-1;
for(ll i=0; i<n; i++){
if(a[i]!=mxa) continue;
if(l==-1) l=i;
r=i;
}
ll ans=0, curr=0;
for(ll i=1; i<=l; i++){
ans++;
curr++;
ans+=max(0LL, a[i]-curr);
curr=a[i];
}
ans+=(r-l);
curr=0;
for(ll i=n-2; i>=r; i--){
ans++;
curr++;
ans+=max(0LL, a[i]-curr);
curr=a[i];
}
cout << ans;
}
int main() {
int tc=1;
// cin >> tc;
for(int i=1; i<=tc; i++){
// cout << '#' << i << endl;
f();
if(i!=tc) cout << 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... |