#C7874. Bake Sale Change
Bake Sale Change
Bake Sale Change
At a bake sale, each participant buys an item costing $7. Participants pay using one of the three bill denominations: $7, $14, or $21. When a participant pays with a $14 bill, they require change of $7. Likewise, when they pay with a $21 bill, they require change of $14. Initially, the seller has no money. As payments come in sequentially, the seller collects the bills and must provide the correct change immediately if required.
Your task is to determine if it is possible to provide correct change to every participant given the sequence of payments.
The change for a $21 bill can be given in one of the following ways:
- If the seller has one $14 bill and one $7 bill, they can use these.
- Otherwise, if the seller has at least three $7 bills, they can use three $7 bills.
Formally, let the number of $7, $14, and $21 bills available at any moment be denoted by S7, S14, and S21 respectively. Initially, S7 = S14 = S21 = 0. For each payment:
- If the payment is $7: S7 increments by 1.
- If the payment is $14: the seller must use one $7 (i.e. S7 > 0), then update S7 -= 1 and S14 += 1.
- If the payment is $21: the seller must provide $14 in change either by using one $14 and one $7 (if available) or by using three $7 bills.
inputFormat
The input is read from stdin and contains two lines:
- The first line contains a single integer N — the number of participants.
- The second line contains N space-separated integers representing the bill each participant uses for payment. Each bill is one of the values 7, 14, or 21.
outputFormat
Output to stdout a single line: True
if it is possible to provide the correct change to every participant, and False
otherwise.
4
7 14 7 21
True