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.

Date AuthorLanguageStatus Score
Feb 7, 2024 01:44korey-skorey-sRUSTrust-1.75.0Success648,837
Feb 7, 2024 01:22korey-skorey-sRUSTrust-1.75.0Error
Feb 7, 2024 01:12korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 19:37parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 19:09korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 18:56korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 18:56korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 18:24korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 18:18korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 17:48korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 17:34parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 17:33parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 16:05korey-skorey-sRUSTrust-1.75.0Success1,280,306
Feb 6, 2024 16:03korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 16:01korey-skorey-sRUSTrust-1.75.0Success1,252,085
Feb 6, 2024 15:26korey-skorey-sRUSTrust-1.75.0Success272,849
Feb 6, 2024 14:36parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 14:28parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 14:21parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 13:46parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 12:51parastromparastromRUSTrust-1.74.0Success2,833,321
Feb 6, 2024 11:28parastromparastromRUSTrust-1.74.0Error
Feb 6, 2024 10:07Tejas G.Tejas G.CPPg++9.4.0Error
Feb 6, 2024 10:06Tejas G.Tejas G.CPPg++9.4.0Error
Feb 6, 2024 09:47Tejas G.Tejas G.CPPg++9.4.0Error
Feb 6, 2024 09:33Tejas G.Tejas G.CPPg++9.4.0Success183,636+15.75 RP
Feb 6, 2024 09:23Tejas G.Tejas G.CPPclang++10.0.0Success258,341+17.82 RP
Feb 6, 2024 09:21Tejas G.Tejas G.CPPg++9.4.0Error
Feb 6, 2024 09:20Tejas G.Tejas G.CPPg++9.4.0Success666,729
Feb 6, 2024 05:59Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 05:45Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 05:42Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 05:39Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 05:38Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 05:31Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 6, 2024 02:34korey-skorey-sRUSTrust-1.75.0Success268,896+5.88 RP
Feb 6, 2024 02:29korey-skorey-sRUSTrust-1.75.0Error
Feb 6, 2024 01:31korey-skorey-sRUSTrust-1.75.0Success319,441+24.85 RP
Feb 6, 2024 01:13korey-skorey-sRUSTrust-1.75.0Success1,548,956+0.52 RP
Feb 6, 2024 00:46korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:44korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:39korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:38korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:37korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:28korey-skorey-sCPPg++9.4.0Error
Feb 6, 2024 00:12Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 5, 2024 23:53korey-skorey-sRUSTrust-1.75.0Success6,551,014
Feb 5, 2024 23:42Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 5, 2024 16:56parastromparastromRUSTrust-1.74.0Error
Feb 5, 2024 16:39korey-skorey-sRUSTrust-1.75.0Success6,719,457
Feb 5, 2024 16:36korey-skorey-sRUSTrust-1.75.0Success7,828,003
Feb 5, 2024 16:23parastromparastromRUSTrust-1.74.0Error
Feb 5, 2024 16:21korey-skorey-sRUSTrust-1.75.0Error
Feb 5, 2024 14:12korey-skorey-sRUSTrust-1.75.0Success1,685,138+0.02 RP
Feb 5, 2024 14:11korey-skorey-sRUSTrust-1.75.0Success1,689,457+5.92 RP
Feb 5, 2024 00:060xkake0xkakeRUSTrust-1.75.0Success197,671
Feb 5, 2024 00:050xkake0xkakeRUSTrust-1.75.0Error
Feb 5, 2024 00:030xkake0xkakeRUSTrust-1.75.0Success167,082+0.09 RP
Feb 5, 2024 00:030xkake0xkakeRUSTrust-1.75.0Success169,124
Feb 4, 2024 23:560xkake0xkakeRUSTrust-1.70.0Success167,868
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.75.0Success168,988
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.75.0Success168,430
Feb 4, 2024 23:550xkake0xkakeRUSTrust-1.74.0Success168,270
Feb 4, 2024 23:530xkake0xkakeRUSTrust-1.74.0Success167,343+0.01 RP
Feb 4, 2024 23:520xkake0xkakeRUSTrust-1.74.0Success167,362+0.05 RP
Feb 4, 2024 23:510xkake0xkakeRUSTrust-1.74.0Success329,479
Feb 4, 2024 23:480xkake0xkakeRUSTrust-1.74.0Success175,557
Feb 4, 2024 23:470xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 23:440xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 23:390xkake0xkakeRUSTrust-1.74.0Success167,499+0.38 RP
Feb 4, 2024 23:370xkake0xkakeRUSTrust-1.74.0Success168,571+9.66 RP
Feb 4, 2024 23:270xkake0xkakeRUSTrust-1.74.0Success201,357+1.01 RP
Feb 4, 2024 16:390xkake0xkakeRUSTrust-1.74.0Success207,112
Feb 4, 2024 16:390xkake0xkakeRUSTrust-1.74.0Success208,246
Feb 4, 2024 16:380xkake0xkakeRUSTrust-1.74.0Success240,837
Feb 4, 2024 16:300xkake0xkakeRUSTrust-1.74.0Success207,065
Feb 4, 2024 16:250xkake0xkakeRUSTrust-1.74.0Success206,490
Feb 4, 2024 16:180xkake0xkakeRUSTrust-1.74.0Success205,527+0.22 RP
Feb 4, 2024 16:180xkake0xkakeRUSTrust-1.74.0Success206,475+0.03 RP
Feb 4, 2024 16:110xkake0xkakeRUSTrust-1.74.0Success206,600+25.84 RP
Feb 4, 2024 16:030xkake0xkakeRUSTrust-1.74.0Error
Feb 4, 2024 15:540xkake0xkakeRUSTrust-1.74.0Success2,395,972
Feb 4, 2024 15:20Tejas G.Tejas G.CPPclang++10.0.0Success1,092,572
Feb 4, 2024 15:16Tejas G.Tejas G.CPPclang++10.0.0Success1,527,325
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Success634,010
Feb 4, 2024 15:15Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:14Tejas G.Tejas G.RUSTrust-1.75.0Error
Feb 4, 2024 15:13Tejas G.Tejas G.RUSTrust-1.75.0Success478,825+1.03 RP
Feb 4, 2024 15:13Tejas G.Tejas G.RUSTrust-1.75.0Success625,201
Feb 4, 2024 15:13Tejas G.Tejas G.CPPg++9.4.0Success1,506,089
Feb 4, 2024 14:55Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 14:53Tejas G.Tejas G.CPPg++9.4.0Error
Feb 4, 2024 13:09Tejas G.Tejas G.CPPg++9.4.0Success712,786
Feb 4, 2024 13:09Tejas G.Tejas G.CPPclang++10.0.0Success504,044
Feb 4, 2024 13:08Tejas G.Tejas G.CPPclang++10.0.0Success503,741+0.01 RP
Feb 4, 2024 12:41Tejas G.Tejas G.CPPclang++10.0.0Success503,973+6.28 RP
Feb 4, 2024 12:39Tejas G.Tejas G.CPPg++9.4.0Success738,128
Feb 4, 2024 12:25Tejas G.Tejas G.CPPg++9.4.0Success737,467+0.06 RP
Feb 4, 2024 11:01Tejas G.Tejas G.CPPg++9.4.0Error