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 listOct 17, 2024 12:41matsuoka-601matsuoka-601Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 17, 2024 12:4000000Error: expected "962899 ", got "950910 "stderr
2Oct 17, 2024 12:4524,40029,501,31913,142,0001,010,0002,535,424
3Oct 17, 2024 12:4125,45332,545,17914,763,00002,535,424
4Oct 17, 2024 12:4525,71036,937,14714,912,00002,535,424
5Oct 17, 2024 12:4127,41941,125,79415,903,00002,437,120
6Oct 17, 2024 12:4527,62238,647,60315,020,0001,001,0002,273,280
7Oct 17, 2024 12:401,966,5881,166,927,1081,139,622,000999,0002,482,176
8Oct 17, 2024 12:401,968,7521,166,714,0321,139,877,0001,999,0002,371,584
9Oct 17, 2024 12:411,970,4641,164,640,6891,142,869,00002,371,584
10Oct 17, 2024 12:411,971,1241,174,649,4821,139,255,0003,997,0002,379,776
11Oct 17, 2024 12:451,973,5451,176,984,8311,139,658,0004,998,0002,367,488
12Oct 17, 2024 12:411,974,5381,177,843,9671,140,236,0004,996,0002,482,176
13Oct 17, 2024 12:451,976,2721,176,816,9821,145,239,000999,0002,273,280
14Oct 17, 2024 12:411,977,6021,172,809,1051,144,012,0002,997,0002,482,176
15Oct 17, 2024 12:411,978,1091,175,222,2181,145,305,0001,998,0002,482,176
16Oct 17, 2024 12:411,978,1191,175,481,1541,145,311,0001,998,0002,535,424
17Oct 17, 2024 12:411,978,2981,176,451,0171,143,416,0003,997,0002,273,280
18Oct 17, 2024 12:411,978,6791,174,197,1291,144,635,0002,999,0002,482,176
19Oct 17, 2024 12:451,978,8901,180,439,1241,145,757,0001,999,0002,535,424
20Oct 17, 2024 12:451,979,0411,175,585,0991,146,845,000999,0002,482,176
21Oct 17, 2024 12:411,979,1411,175,153,5541,145,903,0001,999,0002,482,176
22Oct 17, 2024 12:411,980,1091,173,700,4591,145,465,0002,998,0002,482,176
23Oct 17, 2024 12:451,982,2971,175,166,9891,148,733,000999,0002,482,176
24Oct 17, 2024 12:401,982,5171,170,827,1311,148,861,000999,0002,375,680
25Oct 17, 2024 12:451,984,3141,169,991,6611,150,902,00002,482,176