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 listApr 17, 2024 19:15E SequeiraE SequeiraScore: 77,390Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 17, 2024 19:1575,69369,548,18141,907,0001,995,0002,285,568
2Apr 17, 2024 19:2075,70569,428,12143,909,00002,260,992
3Apr 17, 2024 19:2075,72670,753,90142,923,000998,0002,519,040
4Apr 17, 2024 19:2075,90769,088,38942,025,0002,001,0002,285,568
5Apr 17, 2024 19:1575,93866,259,45042,042,0002,002,0002,510,848
6Apr 17, 2024 19:1576,03879,603,42743,100,0001,002,0002,285,568
7Apr 17, 2024 19:1576,06469,929,07042,112,0002,005,0002,510,848
8Apr 17, 2024 19:1576,16771,184,59342,169,0002,008,0002,510,848
9Apr 17, 2024 19:1776,22664,274,95742,202,0002,009,0002,285,568
10Apr 17, 2024 19:1776,23472,007,09642,207,0002,009,0002,506,752
11Apr 17, 2024 19:1576,45266,180,29343,335,0001,007,0002,510,848
12Apr 17, 2024 19:2076,56265,569,20743,397,0001,009,0002,519,040
13Apr 17, 2024 19:1776,56967,655,48643,401,0001,009,0002,285,568
14Apr 17, 2024 19:1776,77667,437,34243,518,0001,012,0002,285,568
15Apr 17, 2024 19:2076,93176,160,60343,606,0001,014,0002,285,568
16Apr 17, 2024 19:1577,00973,218,91643,650,0001,015,0002,510,848
17Apr 17, 2024 19:1777,14869,671,63943,730,0001,016,0002,285,568
18Apr 17, 2024 19:1777,38672,594,02243,887,000997,0002,285,568
19Apr 17, 2024 19:1577,39073,460,40343,889,000997,0002,285,568
20Apr 17, 2024 19:2077,39568,201,34842,894,0001,995,0002,510,848
21Apr 17, 2024 19:2077,41771,251,86843,905,000997,0002,285,568
22Apr 17, 2024 19:2077,48168,164,52242,942,0001,997,0002,273,280
23Apr 17, 2024 19:1777,54073,313,66942,975,0001,998,0002,285,568
24Apr 17, 2024 19:1777,54767,956,66042,978,0001,999,0002,285,568
25Apr 17, 2024 19:1577,58166,302,38542,998,0001,999,0002,285,568
26Apr 17, 2024 19:1777,72272,134,67243,076,0002,003,0002,260,992
27Apr 17, 2024 19:2077,97863,388,48444,222,0001,005,0002,519,040
28Apr 17, 2024 19:1579,589122,847,81146,161,80502,220,032
29Apr 17, 2024 19:1579,737103,213,99246,247,65302,224,128
30Apr 17, 2024 19:2080,240111,473,01746,539,31602,236,416
31Apr 17, 2024 19:1580,302111,344,62146,575,30402,252,800
32Apr 17, 2024 19:1780,400113,892,03046,631,92202,236,416
33Apr 17, 2024 19:2080,743118,227,18846,830,70702,220,032
34Apr 17, 2024 19:2080,892100,850,11846,917,59102,244,608
35Apr 17, 2024 19:1781,287111,077,14447,146,24302,236,416
36Apr 17, 2024 19:1781,361109,425,08947,189,58102,228,224