#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
long long ans, mn = (long long)1e9;
cin >> n;
int a[n];
vector<long long> pos(n);
vector<long long> neg(n);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 1; i < n; i++){
if(a[i] > a[i-1]){
pos[i] = pos[i-1];
neg[i] = neg[i-1] + a[i] - a[i-1] + 1;
}
else if(a[i] < a[i-1]){
pos[i] = pos[i-1] + a[i-1] - a[i] + 1;
neg[i] = neg[i-1];
}
else{
pos[i] = pos[i-1] + 1;
neg[i] = neg[i-1] + 1;
}
}
for(int k = 0; k < n; k++){
ans = pos[k] + neg[n-k-1];
mn = min(mn,ans);
}
cout << mn << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |