# Need help in Codeforces Problem B (Div 3)

Where am I going wrong?

1 Like

You need to sort the input first with respect to x then with respect to y.

1 Like

Figured it out finally. Wasnâ€™t sorting it correctly.

https://codeforces.com/contest/1294/submission/69418915

a suggestion when you are sorting use a better comparison function.
though i have a doubt how are you taking the array as [(x0,y0],[x1,y1]â€¦]?
and yeah when you sorted according to the first index it ignored the second index.
i know in C++ if you sort a vector of pairs it will sort the first one then it will sort according to the second if first is similar.
I think it applies the same in C++ thatâ€™s why normal sorting works too.

Yep, exactly. I assumed that this happens in python too, but to my surprise it doesnâ€™t (or maybe not in the way I did), and thatâ€™s where I was wrong.

https://codeforces.com/contest/1294/submission/69473257
dude i did the normal sorting and it worked

so what i was saying was when you do

``````    packages = sorted(packages, key=lambda x: x[1])
``````

you are sorting according to the second index.(zero based indexing)
while

``````    packages = sorted(packages)
``````

sorts first index then looks at the second index
thatâ€™s why you got WA in that.

I did use that, but it verdict was failed pretest 3. So I get your point.

If I sort according to second index, it doesnâ€™t take care of the first? Is that what youâ€™re saying?

yes it will ignore the first index.