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 listSep 27, 2023 21:33apuxbtapuxbtScore: 765,429Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 27, 2023 21:29748,445496,248,853434,098,28502,215,936
2Sep 27, 2023 21:27759,010507,252,371440,226,00802,248,704
3Sep 27, 2023 21:28760,713500,184,010441,213,62002,310,144
4Sep 27, 2023 21:27761,363515,070,512441,590,60702,285,568
5Sep 27, 2023 21:25761,857511,273,802441,876,94002,215,936
6Sep 27, 2023 21:28763,354517,510,134442,745,09002,285,568
7Sep 27, 2023 21:31763,373510,501,381442,756,44402,281,472
8Sep 27, 2023 21:41763,378507,461,397442,759,43902,260,992
9Sep 27, 2023 21:41764,976522,650,912443,685,81502,248,704
10Sep 27, 2023 21:31765,404514,264,673443,934,31702,248,704
11Sep 27, 2023 21:25765,429511,102,449443,948,81302,215,936
12Sep 27, 2023 21:33765,638527,401,868444,069,77902,289,664
13Sep 27, 2023 21:25765,791512,834,872444,159,04402,228,224
14Sep 27, 2023 21:27767,215522,764,360444,984,51402,224,128
15Sep 27, 2023 21:41767,492498,140,012445,145,41902,248,704
16Sep 27, 2023 21:28769,322510,528,044446,206,86602,236,416
17Sep 27, 2023 21:31769,330516,031,025446,211,45302,297,856
18Sep 27, 2023 21:29769,695511,355,632446,423,27602,228,224
19Sep 27, 2023 21:33770,600515,396,246446,947,82202,240,512
20Sep 27, 2023 21:29770,685508,131,113446,997,17302,256,896
21Sep 27, 2023 21:33772,738515,167,207448,187,76402,265,088