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 18, 2025 23:57HattonuriHattonuriError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:24Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:24057,736,05849,788,000016,384No answer to stdout. Died or killed?
2Dec 18, 2025 23:4833,49548,726,02019,427,00002,523,136
3Dec 18, 2025 23:4835,40246,531,65819,507,0001,026,0002,527,232
4Dec 18, 2025 23:5735,67942,103,80819,660,0001,034,0002,363,392
5Dec 18, 2025 23:4836,03451,270,36219,905,000995,0002,269,184
6Dec 18, 2025 23:4836,03444,896,58719,905,000995,0002,531,328
7Dec 18, 2025 23:5736,04548,132,36919,911,000995,0002,531,328
8Dec 18, 2025 23:4836,04848,400,91418,917,0001,991,0002,527,232
9Dec 18, 2025 23:4836,17250,496,68819,981,000999,0002,269,184
10Dec 18, 2025 23:4836,20045,691,51918,997,0001,999,0002,367,488
11Dec 18, 2025 23:4836,36448,658,56119,083,0002,008,0002,531,328
12Dec 18, 2025 23:4836,74343,091,83721,311,00002,269,184
13Dec 18, 2025 23:5737,33346,785,19220,622,0001,031,0002,363,392
14Dec 18, 2025 23:5737,76249,444,74320,907,000995,0002,527,232
15Dec 18, 2025 23:5737,81044,661,96720,886,0001,044,0002,527,232
16Dec 18, 2025 23:4838,09149,580,18621,089,0001,004,0002,527,232
17Dec 18, 2025 23:4838,09347,833,57521,090,0001,004,0002,527,232
18Dec 18, 2025 23:5738,26443,137,12421,185,0001,008,0002,281,472
19Dec 18, 2025 23:4838,55347,885,47121,345,0001,016,0002,527,232