Under construction

Still being built. Things may change

Working at the Restaurant

Interview

Time limit1sMemory limit128 MB

Summary
Simulate a two-pile plate stacking strategy that always drops on pile 2 and serves from pile 1, moving plates when pile 1 runs out, and output the exact operation transcript.
Level

Easy3 of 10

Topics
Stack, Simulation, Implementation
Solved
No attempts yet

Problem

Last night, Tom went on a date with a really nice girl. However, he forgot to bring his credit card and had no cash in his wallet, so he ended up working at the restaurant to pay the bill. His task is to take plates from the waiter when they come from the tables, and pass them along when the dishwasher requests them. It is very important that the plates are washed in the same order in which they are brought from the tables; otherwise a plate might wait too long before being washed and leftover food could get stuck. Holding all the plates in his hands is not a great idea, so Tom puts them on a table as soon as the waiter hands them over, and picks them up again when it is time to pass them to the dishwasher. There is room for only two piles of plates on the table, referred to as pile 1 and pile 2, and Tom has only this one table to use.

Tom won last year's SWERC, so he is certainly capable of optimizing for efficiency. Given the sequence of plates and requests he receives, you must output the transcript of the specific way, described below, in which Tom organizes the plates on the table.

Input

The input has several test cases. Each case begins with a line containing a number NN (1≤N≤10001 \le N \le 1000), followed by NN lines, each of which is either DROP m or TAKE m, where m>0m > 0 is the number of plates to drop or take. DROP m means the next event is the waiter bringing mm plates to Tom, so he has to drop them on the table; TAKE m means the next event is Tom taking mm plates from the table and passing them along in the right order. You may assume that Tom never receives a TAKE m instruction when there are fewer than mm plates on the table, and that the sum MM of all values of mm in DROP operations does not exceed 100 000100\,000. Note that there may be plates left on Tom's table after the last request, since Tom might be relieved of his duty before the restaurant closes.

The input ends with a line with N=0N = 0, which must not be processed.

Output

For each test case, output the transcript of the operations Tom performs. Each line is one of the following:

  • DROP 1 m or DROP 2 m (m>0m > 0): take a plate from the waiter and drop it on top of pile 1 (respectively pile 2), repeated mm times in total.
  • TAKE 1 m or TAKE 2 m (m>0m > 0): take a plate from the top of pile 1 (respectively pile 2) and pass it to the dishwasher, repeated mm times in total.
  • MOVE 1->2 m or MOVE 2->1 m (m>0m > 0): take a plate from the top of pile 1 (respectively pile 2) and drop it on top of the other pile, repeated mm times in total.

Tom always drops incoming plates on pile 2 and always serves the dishwasher from pile 1, following exactly this deterministic strategy (you must reproduce its transcript):

  • For each DROP m command, output one line DROP 2 m.
  • For each TAKE m command, processed in order:
    • If pile 1 currently holds a>0a > 0 plates, output TAKE 1 k with k=min⁡(m,a)k = \min(m, a), and decrease the number of plates still needed by kk.
    • If plates are still needed afterwards (pile 1 is now empty), output MOVE 2->1 b, where bb is the current number of plates on pile 2 — this moves every plate from pile 2 onto pile 1 — and then output TAKE 1 r, where rr is the number of plates still needed.

Handle the commands in the exact order they are given. This strategy washes every plate in the same order it arrived, and uses at most 3N3N output lines and at most 3M3M plate movements. Print a blank line between the outputs of different test cases.

Examples1

  1. Example 1

    Input
    3
    DROP 100
    TAKE 50
    TAKE 20
    3
    DROP 3
    DROP 5
    TAKE 8
    0
    
    Expected output
    DROP 2 100
    MOVE 2->1 100
    TAKE 1 50
    TAKE 1 20
    
    DROP 2 3
    DROP 2 5
    MOVE 2->1 8
    TAKE 1 8