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 19:31Wookyung JeongWookyung JeongScore: 60,998Success
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 19:2558,02662,685,97131,616,0002,039,0002,502,656
2Sep 29, 2025 19:2558,27664,709,21228,830,0004,970,0002,359,296
3Sep 29, 2025 19:2858,81257,982,19932,105,0002,006,0002,519,040
4Sep 29, 2025 19:3158,82658,408,53530,105,0004,014,0002,486,272
5Sep 29, 2025 19:3259,14857,463,27629,261,0005,045,0002,408,448
6Sep 29, 2025 19:3260,07960,277,50229,868,0004,978,0002,301,952
7Sep 29, 2025 19:2860,20563,287,33230,929,0003,990,0002,461,696
8Sep 29, 2025 19:2860,29763,940,12728,977,0005,995,0002,441,216
9Sep 29, 2025 19:3160,74356,857,47632,212,0003,019,0002,383,872
10Sep 29, 2025 19:2860,99864,734,26831,336,0004,043,0002,441,216
11Sep 29, 2025 19:3161,90261,624,29933,852,0002,051,0002,457,600
12Sep 29, 2025 19:2862,03357,919,63732,981,0002,998,0002,473,984
13Sep 29, 2025 19:3162,58157,899,68831,256,0005,041,0002,424,832
14Sep 29, 2025 19:3263,74357,601,36231,975,0004,996,0002,322,432
15Sep 29, 2025 19:3164,15360,090,79835,198,0002,011,0002,494,464
16Sep 29, 2025 19:3164,33362,293,15734,288,0003,025,0002,273,280
17Sep 29, 2025 19:2864,41260,000,36234,330,0003,029,0002,355,200
18Sep 29, 2025 19:2565,58661,154,62934,036,0004,004,0002,379,776