Problem link — https://codedrills.io/contests/amrita-icpc-practice-session-7/problems/alibaba-and-thieves There is an editorial, but it just describes the algorithm instead of the idea (or I just don't get it). Can anyone please describe why is this correct? Also, if there are more problems where we need to sort some intervals and do something greedily, please share.

At a given time say you have some intervals that can be taken. Then you want to take the one that ends first because others that end later can be taken later.

My approach was iterating over time, and whenever it is possible to include one or more coins, greedily choose one which has the least right range, because other with greater right range will still be available in next second. Though, time <= 1e9 but whenever we don't have any coin possible at that particular time, simply move to the nearest upcoming left range.

Thank you, I think I have finally figured out how to implement it.