#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
int n, m; cin >> n >> m;
int alt = 0, ans = n - 1;
int a[n + 1];
int mx[n + 1];
for (int i = 1; i <= n; i++){
cin >> a[i];
}
mx[n] = 0;
for (int i = n - 1; i >= 1; i--){
mx[i] = max(mx[i + 1], a[i]);
}
for (int i = 1; i < n; i++){
if (mx[i + 1] > alt) alt++;
if (mx[i + 1] < alt) alt--;
if (a[i + 1] > alt){
if (a[i + 1] > alt){
ans += a[i + 1] - alt;
alt = a[i + 1];
}
}
}
cout << ans + alt << 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... |