Order book Yuriy Lyfenko

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Back to listSep 29, 2025 08:36Wookyung JeongWookyung JeongScore: 60,310Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 29, 2025 08:3756,94060,189,00729,022,0004,003,0002,449,408
2Sep 29, 2025 08:3757,37659,957,88329,245,0004,033,0002,420,736
3Sep 29, 2025 08:3657,81257,220,08226,419,0007,112,0002,498,560
4Sep 29, 2025 08:3658,83656,554,24230,111,0004,014,0002,392,064
5Sep 29, 2025 08:3459,36262,794,82930,380,0004,050,0002,322,432
6Sep 29, 2025 08:3459,47456,410,29631,452,0003,043,0002,494,464
7Sep 29, 2025 08:3560,15962,915,21529,908,0004,984,0002,375,680
8Sep 29, 2025 08:3660,31062,370,22030,983,0003,997,0002,424,832
9Sep 29, 2025 08:3560,36958,614,62131,013,0004,001,0002,449,408
10Sep 29, 2025 08:3560,73862,721,25230,196,0005,032,0002,408,448
11Sep 29, 2025 08:3660,76257,435,28731,215,0004,027,0002,359,296
12Sep 29, 2025 08:3660,85757,957,97430,255,0005,042,0002,338,816
13Sep 29, 2025 08:3661,11958,392,62430,385,0005,064,0002,400,256
14Sep 29, 2025 08:3762,24858,210,06734,099,0002,005,0002,359,296
15Sep 29, 2025 08:3464,14060,482,06833,180,0004,021,0002,355,200