Problems > Exact Change

Exact Change

Difficulty: 1300 3000 ms 128 MB

greedy

Lemonade costs 5. Customers pay with 5, 10 or 20 bills in the given order, and you start with no money. Can you give everyone correct change?

Input: the first line has N; the second line has the N bills in order.

Output: YES or NO.

Constraints:

  • 1 ≤ N ≤ 105
  • bills are 5, 10 or 20

Sample Tests

Input 1
5
5 5 5 10 20
Output 1
YES
Input 2
3
10 10 10
Output 2
NO

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.