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:27HattonuriHattonuriError
Source Code

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

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