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 16:59Wookyung JeongWookyung JeongError
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Jul 5, 2026 21:07Victor Mercklé10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 5, 2026 21:07031,300,58821,921,000016,384No answer to stdout. Died or killed?
2Dec 18, 2025 16:5931,28330,159,01416,128,0002,016,0002,314,240
3Dec 18, 2025 16:5931,85242,531,29818,474,00002,510,848
4Dec 18, 2025 17:0031,91648,398,20217,483,0001,028,0002,510,848
5Dec 18, 2025 16:5932,00740,521,96617,533,0001,031,0002,478,080
6Dec 18, 2025 16:5932,59546,542,58316,915,0001,990,0002,502,656
7Dec 18, 2025 16:5932,72142,811,15416,981,0001,997,0002,289,664
8Dec 18, 2025 17:0732,76445,372,08017,003,0002,000,0002,506,752
9Dec 18, 2025 16:5932,97243,265,78818,118,0001,006,0002,498,560
10Dec 18, 2025 17:0033,03147,768,11318,150,0001,008,0002,506,752
11Dec 18, 2025 17:0733,04144,169,80717,147,0002,017,0002,437,120
12Dec 18, 2025 17:0033,06945,327,68918,171,0001,009,0002,502,656
13Dec 18, 2025 16:5933,14342,683,09017,200,0002,023,0002,498,560
14Dec 18, 2025 16:5933,19741,365,98917,228,0002,026,0002,490,368
15Dec 18, 2025 16:5933,21748,079,57317,238,0002,028,0002,510,848
16Dec 18, 2025 16:5933,21745,073,79818,252,0001,014,0002,453,504
17Dec 18, 2025 16:5933,54140,079,44018,431,0001,023,0002,510,848
18Dec 18, 2025 16:5933,74039,043,90619,569,00002,441,216
19Dec 18, 2025 17:0733,78441,031,10018,564,0001,031,0002,379,776