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 listJan 22, 2025 23:07Code HintsCode HintsScore: 115,590Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 22, 2025 23:08113,14390,836,13764,614,0001,009,0002,416,640
2Jan 22, 2025 23:09113,20992,358,42063,641,0002,020,0002,535,424
3Jan 22, 2025 23:07113,76491,320,29163,984,0001,999,0002,412,544
4Jan 22, 2025 23:07114,51099,138,72864,404,0002,012,0002,535,424
5Jan 22, 2025 23:08114,63890,406,04966,490,00002,510,848
6Jan 23, 2025 00:51114,69192,860,18964,506,0002,015,0002,273,280
7Jan 22, 2025 23:08114,95992,907,27964,656,0002,020,0002,531,328
8Jan 22, 2025 23:09114,99896,685,37764,678,0002,021,0002,412,544
9Jan 22, 2025 23:09115,05096,133,64865,718,0001,011,0002,535,424
10Jan 23, 2025 00:51115,32296,929,37564,891,0001,996,0002,273,280
11Jan 22, 2025 23:07115,32499,199,44664,892,0001,996,0002,412,544
12Jan 22, 2025 23:08115,33194,665,96664,896,0001,996,0002,416,640
13Jan 22, 2025 23:07115,34096,555,05365,884,0001,013,0002,535,424
14Jan 22, 2025 23:08115,34191,162,10666,898,00002,408,448
15Jan 22, 2025 23:09115,34892,052,55364,905,0001,997,0002,412,544
16Jan 22, 2025 23:07115,48193,485,08164,980,0001,999,0002,408,448
17Jan 22, 2025 23:09115,59092,989,89165,041,0002,001,0002,523,136
18Jan 22, 2025 23:08115,64188,980,38265,070,0002,002,0002,535,424
19Jan 22, 2025 23:20115,83890,293,77964,178,0003,008,0002,535,424
20Jan 22, 2025 23:07115,91694,460,94165,225,0002,006,0002,535,424
21Jan 23, 2025 00:51116,12496,668,87065,342,0002,010,0002,535,424
22Jan 22, 2025 23:07116,28688,094,35366,440,0001,006,0002,416,640
23Jan 22, 2025 23:07116,31091,117,62465,447,0002,013,0002,535,424
24Jan 22, 2025 23:08116,37897,453,13365,485,0002,014,0002,523,136
25Jan 22, 2025 23:08116,38889,770,74867,505,00002,416,640
26Jan 23, 2025 01:22116,50292,236,49866,563,0001,008,0002,412,544
27Jan 22, 2025 23:20116,51295,589,93465,560,0002,017,0002,420,736
28Jan 22, 2025 23:08116,51993,502,37765,564,0002,017,0002,273,280
29Jan 22, 2025 23:07116,60799,015,78366,623,0001,009,0002,535,424
30Jan 23, 2025 01:22117,02497,891,24765,878,0001,996,0002,412,544
31Jan 22, 2025 23:09117,05795,990,09964,898,0002,995,0002,535,424
32Jan 23, 2025 01:22117,28192,314,15265,022,0003,001,0002,531,328
33Jan 22, 2025 23:20117,57289,946,19165,184,0003,008,0002,535,424