All problems

Problem 244

Lemonade Change

Easy · 10 pts · Array, Greedy

Problem

A lemonade costs 5. Customers stand in a queue and pay one at a time with a 5, 10 or 20 note. Each customer must receive the correct change, using only the notes you have collected from earlier customers; you start with no money. Decide whether you can give every customer correct change. Keep counts of your 5 and 10 notes. A 10 needs one 5 back. A 20 needs 15 back: prefer one 10 and one 5, because 5 notes are more useful later, and use three 5's only if you have no 10.

Input

First line: N. Second line: the N notes in order.

Output

Print YES or NO.

Constraints

1 <= N <= 100000; each note is 5, 10 or 20

Example input

5
5 5 5 10 20

Example output

YES

Explanation

The first three customers pay with 5's, so you hold three 5's. The fourth pays 10 and gets a 5 back. The fifth pays 20 and gets the 10 and a 5 back. Everyone receives change, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Assign Cookies Next: Maximum Non-Overlapping Meetings