Best-Fit Memory Allocator
An embedded runtime manages a single contiguous heap of M bytes with addresses 0 to M − 1. At any moment the heap is split into allocated blocks and free blocks. Free blocks are always kept maximal: two free blocks never touch each other. At the start the whole heap is one free block of M bytes at address 0.
The runtime uses a best-fit policy. It processes q requests in order:
ALLOC k: requestkbytes. Among all free blocks of size at leastk, take the one with the smallest size; if several have that size, take the one with the lowest start address. The allocation occupies the firstkbytes of that block, starting at its start address, and the remaining bytes (if any) stay a free block right after it. If no free block has at leastkbytes, the request fails and nothing changes.FREE id: release the allocation made by theid-thALLOCrequest. Its bytes become free and merge with a free block that ends exactly where it starts and with a free block that starts exactly where it ends, if such blocks exist.
ALLOC requests are numbered 1, 2, 3, … in the order they appear, counting failed ones too.
For every ALLOC request print the address it returned, or -1 if it failed. After all requests, print the number of free blocks and the size of the largest one.
Input
The first line contains two integers M and q (1 ≤ M ≤ 10^18, 1 ≤ q ≤ 2·10^5): the heap size in bytes and the number of requests.
Each of the next q lines is either ALLOC k (1 ≤ k ≤ 10^18) or FREE id. For every FREE id it is guaranteed that the id-th ALLOC request appears earlier, that it succeeded, and that its allocation has not been freed yet.
Output
For each ALLOC request, in order, print one line with its start address, or -1 if it failed.
Then print one line with two integers: the number of free blocks after all requests, and the size in bytes of the largest free block (0 0 if there are no free blocks).
Addresses and sizes can exceed 2^31; use 64-bit integers.
Notes
In the first example, after FREE 1 and FREE 3 the free blocks are [0, 30), [50, 60) and [85, 100) (sizes 30, 10 and 15). ALLOC 12 takes the 15-byte block at address 85 even though the block at 0 comes first, and ALLOC 8 takes the 10-byte block at 50. FREE 2 releases [30, 50), which merges with [0, 30); the block at 50 is allocated again, so the merge stops there. The largest free block then has 50 bytes, so ALLOC 60 fails.
In the second example, after FREE 1 and FREE 3 there are free blocks of 3 bytes at 0 and 4 bytes at 6 ([6, 9) merged with the leftover byte at 9). ALLOC 3 fits exactly at address 0. The 6th request fails, and the final FREE 2 joins [0, 3), [3, 6) and [6, 10) into one block of 10 bytes.