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 listDec 20, 2025 15:27HattonuriHattonuriScore: 36,610Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 15:3434,43340,516,96918,973,000998,0002,527,232
2Dec 20, 2025 15:3634,79539,330,59119,172,0001,009,0002,527,232
3Dec 20, 2025 15:3434,92144,058,58119,242,0001,012,0002,363,392
4Dec 20, 2025 15:2935,27642,792,51419,437,0001,023,0002,371,584
5Dec 20, 2025 15:3635,80945,326,45919,731,0001,038,0002,523,136
6Dec 20, 2025 15:3436,04852,516,70919,913,000995,0002,527,232
7Dec 20, 2025 15:3436,08342,638,88820,928,00002,363,392
8Dec 20, 2025 15:3636,37244,246,68420,092,0001,004,0002,531,328
9Dec 20, 2025 15:2736,41040,117,94720,113,0001,005,0002,269,184
10Dec 20, 2025 15:3436,53339,931,16120,180,0001,009,0002,367,488
11Dec 20, 2025 15:3436,61042,918,51019,212,0002,022,0002,527,232
12Dec 20, 2025 15:3436,76039,163,55720,306,0001,015,0002,519,040
13Dec 20, 2025 15:3436,95053,652,57720,411,0001,020,0002,519,040
14Dec 20, 2025 15:2936,95248,664,19321,432,00002,523,136
15Dec 20, 2025 15:4237,01249,309,82121,467,00002,269,184
16Dec 20, 2025 15:4237,04848,580,84521,488,00002,527,232
17Dec 20, 2025 15:2937,14140,254,21721,542,00002,523,136
18Dec 20, 2025 15:3437,26246,117,17220,583,0001,029,0002,273,280
19Dec 20, 2025 15:2737,42442,467,69421,706,00002,527,232
20Dec 20, 2025 15:4237,45046,284,86520,687,0001,034,0002,519,040
21Dec 20, 2025 15:2737,96043,834,49721,017,0001,000,0002,535,424