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 28, 2025 20:15imotw2imotw2Error
Source Code

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

Challenge History
DateChallengerCostTypeStatus
Sep 11, 2026 14:12Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 11, 2026 14:16016,948,42806,875,00020,480No answer to stdout. Died or killed?
2Dec 28, 2025 20:1247,47448,064,64323,456,0004,079,0002,523,136
3Dec 28, 2025 20:1348,71652,384,05123,210,0005,045,0002,359,296
4Dec 28, 2025 20:1248,79750,760,59024,259,0004,043,0002,461,696
5Dec 28, 2025 20:1248,79748,655,45623,248,0005,054,0002,265,088
6Dec 28, 2025 20:1549,12850,506,19823,406,0005,088,0002,326,528
7Dec 28, 2025 20:1349,19052,299,49423,436,0005,094,0002,469,888
8Dec 28, 2025 20:1349,19846,349,21024,459,0004,076,0002,510,848
9Dec 28, 2025 20:1249,26749,691,91723,473,0005,102,0002,506,752
10Dec 28, 2025 20:1249,56256,586,87124,640,0004,106,0002,506,752
11Dec 28, 2025 20:1349,80057,621,24324,900,0003,984,0002,400,256
12Dec 28, 2025 20:1349,81961,161,94724,910,0003,985,0002,301,952
13Dec 28, 2025 20:1349,84151,869,14524,921,0003,987,0002,396,160
14Dec 28, 2025 22:5649,97661,484,58423,989,0004,997,0002,490,368
15Dec 28, 2025 22:5650,05553,817,57425,028,0004,004,0002,506,752
16Dec 28, 2025 22:5650,08652,443,07024,042,0005,008,0002,318,336
17Dec 28, 2025 20:4250,09152,799,29425,046,0004,007,0002,347,008
18Dec 28, 2025 20:1350,22953,839,21125,115,0004,018,0002,453,504
19Dec 28, 2025 22:5650,55752,897,07325,279,0004,044,0002,494,464
20Dec 28, 2025 20:1250,59151,899,89925,296,0004,047,0002,338,816
21Dec 28, 2025 20:1350,95255,928,10924,457,0005,095,0002,310,144
22Dec 28, 2025 20:1250,96253,273,84524,462,0005,096,0002,269,184
23Dec 28, 2025 20:4251,05355,847,99724,506,0005,105,0002,445,312
24Dec 28, 2025 20:1251,51959,216,35324,901,0004,980,0002,457,600
25Dec 28, 2025 20:1351,59053,649,83626,827,0003,095,0002,281,472
26Dec 28, 2025 20:1251,77956,371,94126,028,0004,004,0002,367,488
27Dec 28, 2025 20:1552,10251,666,90825,183,0005,036,0002,265,088
28Dec 28, 2025 22:5652,36752,620,19024,299,0006,074,0002,273,280
29Dec 28, 2025 20:1552,42956,078,98626,355,0004,054,0002,502,656
30Dec 28, 2025 22:5652,77260,659,44824,487,0006,121,0002,310,144
31Dec 28, 2025 20:4253,01457,949,22725,624,0005,124,0002,424,832