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 listMar 18, 2026 20:05Yuriy LyfenkoYuriy LyfenkoScore: 4,453Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 18, 2026 20:133,66918,619,4041,064,0001,064,0002,396,160
2Mar 18, 2026 20:133,84814,895,9732,232,00002,379,776
3Mar 18, 2026 20:133,95317,400,0052,293,00002,351,104
4Mar 18, 2026 20:134,03114,872,6391,169,0001,169,0002,379,776
5Mar 18, 2026 20:064,04519,120,2332,346,00002,379,776
6Mar 18, 2026 20:064,04821,009,1532,348,00002,396,160
7Mar 18, 2026 20:134,05020,316,2432,349,00002,269,184
8Mar 18, 2026 20:134,05519,453,97002,352,0002,375,680
9Mar 18, 2026 20:134,08622,904,5741,185,0001,185,0002,342,912
10Mar 18, 2026 20:074,18419,908,6572,427,00002,387,968
11Mar 18, 2026 20:134,30319,024,7891,248,0001,248,0002,322,432
12Mar 18, 2026 20:064,35720,068,3012,527,00002,363,392
13Mar 18, 2026 20:064,36924,310,6182,534,00002,367,488
14Mar 18, 2026 20:134,38619,312,5032,544,00002,318,336
15Mar 18, 2026 20:064,40719,506,4592,556,00002,359,296
16Mar 18, 2026 20:074,45317,655,5142,583,00002,359,296
17Mar 18, 2026 20:064,53121,186,1031,314,0001,314,0002,383,872
18Mar 18, 2026 20:064,58422,054,1622,659,00002,322,432
19Mar 18, 2026 20:074,62120,860,0311,340,0001,340,0002,359,296
20Mar 18, 2026 20:064,65921,071,5702,702,00002,531,328
21Mar 18, 2026 20:064,89715,891,9232,840,00002,318,336
22Mar 18, 2026 20:056,11623,699,3503,547,00002,383,872
23Mar 18, 2026 20:056,15919,037,1973,572,00002,531,328
24Mar 18, 2026 20:056,49120,633,6942,510,0001,255,0002,367,488
25Mar 18, 2026 20:056,49719,509,9633,768,00002,367,488
26Mar 18, 2026 20:056,58618,676,8313,820,00002,342,912
27Mar 18, 2026 20:056,96919,976,6142,021,0002,021,0002,531,328
28Mar 18, 2026 20:057,03422,847,8113,060,0001,020,0002,523,136
29Mar 18, 2026 20:057,13320,639,6953,103,0001,034,0002,539,520
30Mar 18, 2026 20:057,15924,690,3012,076,0002,076,0002,318,336