Submission #309541

#TimeUsernameProblemLanguageResultExecution timeMemory
309541kesochel_Exercise Deadlines (CCO20_day1problem2)C++14
17 / 25
20 ms384 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...