#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int s[300000],a[300000];
vector <int> v[200005];
priority_queue <int> mx;
int n,i;
long long Ans;
int prev(int u){
return u&(u-1);
}
int next(int u){
return 2*u-(u&(u-1));
}
int sum(int x, int y){
int ans=0;
while(y>0){
ans+=s[y];
y=prev(y);
}
while(x>0){
ans-=s[x];
x=prev(x);
}
return ans;
}
void update(int u, long long d){
while (u<=n){
s[u]+=d;
u=next(u);
}
}
main(){
cin>>n;
for(i=1; i<=n; i++){
cin>>a[i];
v[a[i]].push_back(i);
update(i,1);
}
for(i=n; i>0; i--){
for(int j=0; j<v[i].size();j++){
mx.push(v[i][j]);
}
if(mx.size()==0) {
Ans=-1;
break;
}
else{
int near=mx.top();
Ans+=sum(near,n);
update(near,-1);
mx.pop();
}
}
cout<<Ans<<endl;
}
Compilation message
Main.cpp:35:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
35 | main(){
| ^
Main.cpp: In function 'int main()':
Main.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int j=0; j<v[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5248 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5248 KB |
Output is correct |
9 |
Correct |
7 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
7 ms |
5248 KB |
Output is correct |
12 |
Correct |
7 ms |
5248 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
7 ms |
5120 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5120 KB |
Output is correct |
29 |
Correct |
7 ms |
5120 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
7 ms |
5120 KB |
Output is correct |
32 |
Correct |
8 ms |
5120 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
7 ms |
5120 KB |
Output is correct |
35 |
Correct |
7 ms |
5120 KB |
Output is correct |
36 |
Correct |
7 ms |
5120 KB |
Output is correct |
37 |
Correct |
7 ms |
5248 KB |
Output is correct |
38 |
Correct |
7 ms |
5120 KB |
Output is correct |
39 |
Correct |
7 ms |
5120 KB |
Output is correct |
40 |
Correct |
7 ms |
5120 KB |
Output is correct |
41 |
Correct |
7 ms |
5120 KB |
Output is correct |
42 |
Correct |
7 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
4992 KB |
Output is correct |
45 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5248 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5248 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5248 KB |
Output is correct |
9 |
Correct |
7 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
7 ms |
5248 KB |
Output is correct |
12 |
Correct |
7 ms |
5248 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
7 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
7 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
7 ms |
5120 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
8 ms |
5120 KB |
Output is correct |
29 |
Correct |
7 ms |
5120 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
7 ms |
5120 KB |
Output is correct |
32 |
Correct |
8 ms |
5120 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
7 ms |
5120 KB |
Output is correct |
35 |
Correct |
7 ms |
5120 KB |
Output is correct |
36 |
Correct |
7 ms |
5120 KB |
Output is correct |
37 |
Correct |
7 ms |
5248 KB |
Output is correct |
38 |
Correct |
7 ms |
5120 KB |
Output is correct |
39 |
Correct |
7 ms |
5120 KB |
Output is correct |
40 |
Correct |
7 ms |
5120 KB |
Output is correct |
41 |
Correct |
7 ms |
5120 KB |
Output is correct |
42 |
Correct |
7 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
4992 KB |
Output is correct |
45 |
Correct |
4 ms |
4992 KB |
Output is correct |
46 |
Correct |
208 ms |
10492 KB |
Output is correct |
47 |
Correct |
214 ms |
11768 KB |
Output is correct |
48 |
Correct |
200 ms |
11896 KB |
Output is correct |
49 |
Correct |
203 ms |
11768 KB |
Output is correct |
50 |
Correct |
209 ms |
11764 KB |
Output is correct |
51 |
Correct |
202 ms |
11888 KB |
Output is correct |
52 |
Correct |
198 ms |
14072 KB |
Output is correct |
53 |
Correct |
198 ms |
14072 KB |
Output is correct |
54 |
Correct |
177 ms |
14072 KB |
Output is correct |
55 |
Correct |
155 ms |
14172 KB |
Output is correct |
56 |
Correct |
172 ms |
14072 KB |
Output is correct |
57 |
Correct |
175 ms |
14100 KB |
Output is correct |
58 |
Correct |
199 ms |
11896 KB |
Output is correct |
59 |
Correct |
191 ms |
11900 KB |
Output is correct |
60 |
Correct |
194 ms |
12024 KB |
Output is correct |
61 |
Correct |
182 ms |
12088 KB |
Output is correct |
62 |
Correct |
158 ms |
11896 KB |
Output is correct |
63 |
Correct |
178 ms |
12024 KB |
Output is correct |
64 |
Correct |
196 ms |
11772 KB |
Output is correct |
65 |
Correct |
200 ms |
11768 KB |
Output is correct |
66 |
Correct |
189 ms |
11896 KB |
Output is correct |
67 |
Correct |
180 ms |
11768 KB |
Output is correct |
68 |
Correct |
190 ms |
11768 KB |
Output is correct |
69 |
Correct |
201 ms |
12012 KB |
Output is correct |
70 |
Correct |
208 ms |
11768 KB |
Output is correct |
71 |
Correct |
204 ms |
11900 KB |
Output is correct |
72 |
Correct |
204 ms |
11896 KB |
Output is correct |
73 |
Correct |
199 ms |
11768 KB |
Output is correct |
74 |
Correct |
174 ms |
11768 KB |
Output is correct |
75 |
Correct |
199 ms |
11896 KB |
Output is correct |
76 |
Correct |
189 ms |
11512 KB |
Output is correct |
77 |
Correct |
172 ms |
10020 KB |
Output is correct |
78 |
Correct |
151 ms |
14072 KB |
Output is correct |
79 |
Correct |
156 ms |
12028 KB |
Output is correct |
80 |
Correct |
195 ms |
11656 KB |
Output is correct |
81 |
Correct |
171 ms |
10112 KB |
Output is correct |
82 |
Correct |
146 ms |
14072 KB |
Output is correct |
83 |
Correct |
157 ms |
11896 KB |
Output is correct |
84 |
Correct |
214 ms |
11896 KB |
Output is correct |
85 |
Correct |
213 ms |
11896 KB |
Output is correct |
86 |
Correct |
217 ms |
12036 KB |
Output is correct |
87 |
Correct |
171 ms |
11512 KB |
Output is correct |
88 |
Correct |
212 ms |
11896 KB |
Output is correct |