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 26, 2024 13:51Louis PonetLouis PonetScore: 519,588Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2024 23:13518,443327,714,254298,700,0001,997,0002,285,568
2Dec 26, 2024 13:51518,517326,927,635296,744,0003,996,0002,285,568
3Dec 26, 2024 13:51518,567325,976,213296,773,0003,996,0002,285,568
4Dec 26, 2024 13:39518,781328,837,559294,896,0005,997,0002,306,048
5Dec 25, 2024 23:13518,928323,015,645297,979,0002,999,0002,285,568
6Dec 26, 2024 13:39519,169330,704,366299,118,0002,000,0002,306,048
7Dec 26, 2024 13:51519,247328,438,694299,162,0002,001,0002,285,568
8Dec 25, 2024 23:13519,295323,672,801300,191,0001,000,0002,285,568
9Dec 26, 2024 13:39519,317325,575,762300,204,0001,000,0002,306,048
10Dec 26, 2024 13:39519,334325,610,875301,214,00002,306,048
11Dec 25, 2024 23:13519,383329,247,542297,239,0004,003,0002,301,952
12Dec 26, 2024 13:39519,500323,656,027299,308,0002,002,0002,285,568
13Dec 26, 2024 13:39519,500323,435,786301,310,00002,285,568
14Dec 25, 2024 23:13519,588326,475,230300,360,0001,001,0002,285,568
15Dec 25, 2024 23:13519,671325,882,677297,404,0004,005,0002,285,568
16Dec 26, 2024 13:39519,695325,761,641300,422,0001,001,0002,297,856
17Dec 26, 2024 13:51519,714330,176,341300,433,0001,001,0002,285,568
18Dec 26, 2024 13:51519,748326,255,270298,450,0003,004,0002,301,952
19Dec 26, 2024 13:51520,140333,989,472299,684,0001,997,0002,285,568
20Dec 25, 2024 23:13520,190326,329,896301,710,00002,285,568
21Dec 26, 2024 13:39520,193329,118,750301,712,00002,306,048
22Dec 26, 2024 13:39520,314326,586,069299,784,0001,998,0002,285,568
23Dec 26, 2024 13:39520,428328,985,848298,850,0002,998,0002,293,760
24Dec 25, 2024 23:13520,783329,803,286300,054,0002,000,0002,285,568
25Dec 26, 2024 13:39521,319327,891,810300,363,0002,002,0002,314,240
26Dec 25, 2024 23:13527,653333,532,533301,039,0005,000,0002,293,760
27Dec 26, 2024 13:39578,252364,863,863332,383,0003,003,0002,297,856