eolymp
bolt
Try our new interface for solving problems
Problems

Memory Manager

Memory Manager

You are working on the compiler of your own brand new programming language and you have already implemented everything except for memory manager (MM). MM is responsible for allocating objects in memory and deleting them when they become unnecessary. In total, there are \textbf{n} consecutive bytes of memory in your computer. Each object requires certain amount of consecutive bytes in memory. During its execution, the program can send queries of two types to MM: 1) Allocate an object of size \textbf{a} bytes. Note that this size can be different for different objects. Two objects cannot have shared memory. 2) Remove the given object, which was previously allocated. You know in advance all \textbf{m} queries, which will be sent to MM. Find the way to process them all or report that it is impossible. It is guaranteed that there will be not more than two queries of the second kind. \InputFile The first line of the input contains two integers \textbf{n} and \textbf{m} (\textbf{1} ≤ \textbf{n} ≤ \textbf{10^5}, \textbf{1} ≤ \textbf{m} ≤ \textbf{10^5}). Each of the next \textbf{m} lines contains two integers \textbf{a} and \textbf{b}. If \textbf{a} = \textbf{1}, then \textbf{b} is equal to the size of an object you need to allocate. If \textbf{a} = \textbf{2}, then \textbf{b} is equal to the index of query which was already processed and means that you should remove an object allocated in that query. It’s guaranteed that the input is correct, e.g. you’ll never be asked to remove an already removed object, etc. \OutputFile If it is impossible to perform all queries, print one line with the word “\textbf{IMPOSSIBLE}” (without quotes). Otherwise, for each query of the first type (the ones with \textbf{a} = \textbf{1}) you should output one number in its own line, which represents the position of the first byte of an allocated object.Bytes in memory are numbered from \textbf{0}. If there are several valid answer sequences, you may output any of them.
Time limit 1 second
Memory limit 64 MiB
Input example #1
10 5
1 5
1 5
2 2
1 5
2 1
Output example #1
5
0
0
Source 2014 ACM-ICPC Ukraine, 2nd Round Ukraine, September 13, Problem E