Masha and Bears - What's wrong with my solution (Codeforces)


I’ve checked for all car such that car[i]>=weight[i] and car[i]<=2*weight[i] . So all bears are now satisfied.

Now misha is causing all the problem with my code I think:

signed main(){
    vi v(3);cin>>v;
    int vm;cin>>vm;
    for(int c22=v[2];c22<=2*v[2];c22++){
        for(int c11=v[1];c11<=2*v[1] && c11>c22;c11++){
            for(int c00=v[0];c00<=2*v[0] && c00>c11;c00++){

                if(c00>=vm && c11>=vm && c22>=vm && c22<=2*vm){
                    return 0;



First of all, you are over complicating things.
you can simply fix size of first two cars and then check the value for third car. If we set first two cars to max value, it is guaranteed that whosoever owns the car will like it and any lesser size person will not like it because v1>v2>v3.
Now we can run a loop for size of smallest car such that masha likes it if no such car exists we print -1 and if masha likes second car then we also print -1.

1 Like

Yes I agree… I just wanted to try it with brute force.
Obviously your solution is better.

can you explain again what this did in your code:

else if(2*a[3]<c[1]){

And also if you can spot any mistake in my code, It’d help.

check for this condition.

1 Like