#include "bits/stdc++.h"
using namespace std;
#define ar array
const int N = 2e5 + 5;
struct ST{
int tree[N];
void add(int i, int v){ i++;
for(;i<N;i+=(i&-i)) tree[i] += v;
}
int get(int i){ i++;
int r=0;
for(;i>0;i-=(i&-i)) r += tree[i];
return r;
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> a(n);
vector<vector<int>> to(n);
for(int i=0;i<n;i++){
cin>>a[i];
to[--a[i]].push_back(i);
}
priority_queue<int> q;
long long res = 0;
for(int i=n-1;~i;i--){
for(auto x : to[i]) q.push(x);
if(q.empty()){
cout<<-1<<"\n";
return 0;
}
int p = q.top(); q.pop();
int j = p + tree.get(p);
res += (i - j);
tree.add(p, -1);
}
cout<<res<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
540 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
608 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
536 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
608 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
608 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
540 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
608 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
536 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
608 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
608 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
82 ms |
11644 KB |
Output is correct |
47 |
Correct |
80 ms |
11496 KB |
Output is correct |
48 |
Correct |
77 ms |
11508 KB |
Output is correct |
49 |
Correct |
87 ms |
11492 KB |
Output is correct |
50 |
Correct |
83 ms |
11568 KB |
Output is correct |
51 |
Correct |
63 ms |
11492 KB |
Output is correct |
52 |
Correct |
89 ms |
14004 KB |
Output is correct |
53 |
Correct |
72 ms |
14048 KB |
Output is correct |
54 |
Correct |
61 ms |
14212 KB |
Output is correct |
55 |
Correct |
38 ms |
14100 KB |
Output is correct |
56 |
Correct |
47 ms |
13980 KB |
Output is correct |
57 |
Correct |
54 ms |
14108 KB |
Output is correct |
58 |
Correct |
60 ms |
11928 KB |
Output is correct |
59 |
Correct |
72 ms |
11852 KB |
Output is correct |
60 |
Correct |
72 ms |
11852 KB |
Output is correct |
61 |
Correct |
53 ms |
11916 KB |
Output is correct |
62 |
Correct |
47 ms |
11904 KB |
Output is correct |
63 |
Correct |
59 ms |
11932 KB |
Output is correct |
64 |
Correct |
77 ms |
11752 KB |
Output is correct |
65 |
Correct |
65 ms |
11760 KB |
Output is correct |
66 |
Correct |
64 ms |
11816 KB |
Output is correct |
67 |
Correct |
74 ms |
11764 KB |
Output is correct |
68 |
Correct |
67 ms |
11724 KB |
Output is correct |
69 |
Correct |
67 ms |
11752 KB |
Output is correct |
70 |
Correct |
71 ms |
11768 KB |
Output is correct |
71 |
Correct |
72 ms |
11680 KB |
Output is correct |
72 |
Correct |
76 ms |
11688 KB |
Output is correct |
73 |
Correct |
74 ms |
11700 KB |
Output is correct |
74 |
Correct |
57 ms |
11712 KB |
Output is correct |
75 |
Correct |
62 ms |
11692 KB |
Output is correct |
76 |
Correct |
59 ms |
11292 KB |
Output is correct |
77 |
Correct |
53 ms |
9876 KB |
Output is correct |
78 |
Correct |
37 ms |
14036 KB |
Output is correct |
79 |
Correct |
43 ms |
11856 KB |
Output is correct |
80 |
Correct |
56 ms |
11336 KB |
Output is correct |
81 |
Correct |
48 ms |
9864 KB |
Output is correct |
82 |
Correct |
34 ms |
14028 KB |
Output is correct |
83 |
Correct |
46 ms |
12004 KB |
Output is correct |
84 |
Correct |
89 ms |
11756 KB |
Output is correct |
85 |
Correct |
70 ms |
11760 KB |
Output is correct |
86 |
Correct |
71 ms |
11728 KB |
Output is correct |
87 |
Correct |
42 ms |
10704 KB |
Output is correct |
88 |
Correct |
77 ms |
11684 KB |
Output is correct |