Aayuushh19's blog

By Aayuushh19, history, 21 month(s) ago, In English

void solve(){ ll n;cin>>n; vll b(n+2); FOR(i,0,n+2){ cin>>b[i]; } ll mx = *max_element(all(b)); sort(b.rbegin(),b.rend());

vll a;
ll c=0;
FOR(i,1,n+3){
    if(b[i]<=mx and c<n){
       a.pb(b[i]);
       mx-=b[i];
        c++;
    }
}
// dbg(a);
if(sz(a)!=n){
    sort(all(b));
    dbg(b);
    ll m = *max_element(all(b));
    vll temp;
    FOR(i,0,n){
        temp.pb(b[i]);
    }
    dbg(temp);
    if(accumulate(all(temp),0)!=m){
        cout<<-1<<endl;
        return;
    }
    for(auto i:temp){
        cout<<i<<" ";
    }
    cout<<endl;
}
else{
    for(auto i:a){
        cout<<i<<" ";
    }
    cout<<endl;
}

}

https://codeforces.com/problemset/problem/1512/D

Why this code is not working in online judge but working in my pc.

  • Vote: I like it
  • -5
  • Vote: I do not like it

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Your vector b have n+2 positions, 0,1,...b+1, but in FOR(I,1,n+3) you trying to access to b[n+2], but that's out of bounds.