You run a stand where every item costs exactly 5. Customers arrive one at a time and each pays with a single bill worth 5, 10, or 20. You must give each customer back the correct change immediately, using only bills you have collected from earlier customers (you start with no cash).
Decide whether you can serve every customer in the given order.
Line 1: an integer n, the number of customers.
Line 2: n space-separated integers, each 5, 10, or 20 — the bill each customer pays with, in arrival order (present whenever n >= 1).
Print YES if you can give correct change to every customer in order, otherwise print NO.
Example 1
Input
5 5 5 10 20 5
Expected
YES
Explanation
The first two pay 5 (you now hold two 5s). The 10 customer gets one 5 back (you hold one 5 and one 10). The 20 customer gets a 10 and a 5 back (you hold nothing). The last pays 5. Everyone got correct change, so YES.
Example 2
Input
3 5 10 20
Expected
NO
Explanation
The 5 leaves you with one 5. The 10 customer takes that 5 as change, leaving you with one 10 and no 5s. The 20 customer needs 15 back, but a lone 10 cannot make 15, so change fails: NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →