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 30, 2025 02:34Yuriy LyfenkoYuriy LyfenkoError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 30, 2025 02:3400000Error: No answer to stdout. Died or killed?stderr
2Mar 30, 2025 02:3400000Error: No answer to stdout. Died or killed?stderr
3Mar 30, 2025 02:3300000Error: No answer to stdout. Died or killed?stderr
4Mar 30, 2025 02:3300000Error: No answer to stdout. Died or killed?stderr
5Mar 30, 2025 02:3300000Error: Exit with code 255: init: 7 mks not enough sharesstderr
6Mar 30, 2025 02:3200000Error: Exit with code 255: init: 8 mks not enough sharesstderr
7Mar 30, 2025 02:3200000Error: Exit with code 255: init: 7 mks not enough sharesstderr
8Mar 29, 2025 23:1100000Error: Exit with code 255: init: 7 mks not enough sharesstderr
9Mar 30, 2025 02:35132,895100,645,02375,077,0002,002,0002,486,272
10Mar 30, 2025 02:35132,959100,833,02176,115,0001,001,0002,486,272
11Mar 30, 2025 02:35133,091100,982,53775,188,0002,005,0002,281,472
12Mar 29, 2025 23:12133,379101,732,08077,360,00002,285,568
13Mar 29, 2025 23:12133,445102,704,22076,393,0001,005,0002,486,272
14Mar 29, 2025 23:11133,49598,350,83476,422,0001,005,0002,486,272
15Mar 29, 2025 23:12133,647101,186,61575,528,0001,987,0002,281,472
16Mar 30, 2025 02:32133,650100,600,13176,511,0001,006,0002,285,568
17Mar 29, 2025 23:11133,75398,946,41177,577,00002,486,272
18Mar 30, 2025 02:35133,848102,588,43576,624,0001,008,0002,334,720
19Mar 29, 2025 23:11133,867104,176,29076,635,0001,008,0002,490,368
20Mar 29, 2025 23:12133,916111,040,80076,663,0001,008,0002,285,568
21Mar 30, 2025 02:35134,190103,618,49376,833,000997,0002,330,624
22Mar 30, 2025 02:35134,203103,681,59876,841,000997,0002,285,568
23Mar 29, 2025 23:11134,205106,113,03976,842,000997,0002,285,568
24Mar 30, 2025 02:35134,219106,479,97075,851,0001,996,0002,478,080
25Mar 29, 2025 23:12134,222104,285,53875,853,0001,996,0002,285,568
26Mar 30, 2025 02:35134,229109,859,13176,855,000998,0002,482,176
27Mar 29, 2025 23:12134,236108,210,00775,861,0001,996,0002,285,568
28Mar 29, 2025 23:11134,240110,329,29475,863,0001,996,0002,281,472
29Mar 29, 2025 23:12134,250108,074,31076,867,000998,0002,482,176
30Mar 30, 2025 02:32134,252103,339,71575,870,0001,996,0002,285,568
31Mar 30, 2025 02:35134,252113,910,13475,870,0001,996,0002,285,568
32Mar 29, 2025 23:11134,283102,285,40175,887,0001,997,0002,494,464
33Mar 29, 2025 23:12134,364107,873,18175,933,0001,998,0002,482,176
34Mar 30, 2025 02:35134,366100,985,87875,934,0001,998,0002,285,568
35Mar 30, 2025 02:32134,369105,786,45075,936,0001,998,0002,285,568
36Mar 29, 2025 23:11134,431102,149,96375,971,0001,999,0002,285,568
37Mar 29, 2025 23:11134,43399,936,50675,972,0001,999,0002,285,568
38Mar 30, 2025 02:35134,636101,205,00376,087,0002,002,0002,490,368
39Mar 29, 2025 23:12134,738101,128,87176,145,0002,003,0002,285,568
40Mar 29, 2025 23:11135,000104,065,56276,293,0002,007,0002,277,376
41Mar 30, 2025 02:35139,540104,565,79278,935,0001,998,0002,285,568
42Mar 30, 2025 02:34204,398144,080,639117,547,0001,004,0002,285,568
43Mar 30, 2025 02:34205,266146,686,584118,054,0001,000,0002,285,568
44Mar 30, 2025 02:34205,459142,282,712117,164,0002,002,0002,424,832