| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 293581 | achibasadzishvili | Exercise Deadlines (CCO20_day1problem2) | C++14 | 160 ms | 27616 KiB | 
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>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,a[500005];
set<ll>st;
set<ll>::iterator it;
ll b[500005],ans;
vector<ll>g[500005];
void add(ll x,ll y){
    while(y<=n){
        b[y]+=x;
    	y+=y&-y;
    }
}
ll sum(ll y){
    ll x=0;
	while(y>0){
        x+=b[y];
        y-=y&-y;
    }
    return x;
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i=1; i<=n; i++){
        cin >> a[i];
        g[a[i]].pb(i);
    }
    
    for(int i=n; i>=1; i--){
        for(int j=0; j<g[i].size(); j++)
            st.insert(g[i][j]);
        if(!st.size()){
            cout << -1;
            return 0;
        }
        it = st.end();
        it--;
        ll ind = (*it);
        ll w = ind;
        st.erase(it);
        ind -= sum(ind);
        ans += i - ind;
        add(1 , w);
    }
    
    cout << ans << '\n';
    
    
    return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
