This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, ans = 0;
cin >> n;
if(n > 5000){
cout << -1 << endl;
return 0;
}
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int k = 0;
for(int i = 0; i < n; i++){
if(a[i] == n){
k++;
}
}
if(k == 0){
cout << -1 << endl;
} else {
for(int i = n - 1; i >= 0; i--){
for(int j = i; j >= 0; j--){
if(a[j] > i){
for(int k = j; k < i; k++){
swap(a[k], a[k+1]);
}
ans += i - j;
break;
}
}
}
for(int i = 0; i < n; i++){
if(a[i] < i + 1){
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |