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;
#define ll long long
#define ar array
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int mxN=2e5;
int n, a[mxN];
vector<int> oc[mxN];
ll ans;
set<int> s;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i=0; i<n; ++i) {
cin >> a[i], --a[i];
oc[a[i]].push_back(i);
}
for (int i=n-1; ~i; --i) {
for (int j : oc[i])
s.insert(j);
if (s.empty()) {
cout << -1;
return 0;
}
int x=*s.rbegin();
s.erase(--s.end());
t.insert(x);
x-=t.order_of_key(x);
ans+=i-x;
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |