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 16, 2024 21:05Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success99,831
Feb 16, 2024 21:04Yuriy LyfenkoYuriy LyfenkoCPPclang++10.0.0Success148,748
Feb 16, 2024 21:04Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,175
Feb 16, 2024 21:02Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,753
Feb 16, 2024 20:58Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success75,383
Feb 16, 2024 20:58Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success75,690
Feb 16, 2024 20:52Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,241
Feb 16, 2024 20:49Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:48Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:47Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:46Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:46Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:42Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success46,787
Feb 16, 2024 20:39Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success46,007
Feb 16, 2024 20:36Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:34Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:34Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:33Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:33Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:31Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:30Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success38,276
Feb 16, 2024 20:30Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success44,845
Feb 16, 2024 20:27Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:24Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:22Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:20Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success43,136
Feb 16, 2024 20:19Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:14Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 20:13Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 19:57Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success39,447
Feb 16, 2024 19:55Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 19:44Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 19:43Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 19:42Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 19:41Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success39,289
Feb 16, 2024 19:40Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 17:55stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:46stdspstdspCPPg++9.4.0Success118,453
Feb 16, 2024 17:45stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:38stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:37stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:35stdspstdspCPPg++9.4.0Success2,897,360
Feb 16, 2024 17:34stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:30stdspstdspCPPg++9.4.0Success118,671
Feb 16, 2024 17:30parastromparastromRUSTrust-1.74.0Success151,652
Feb 16, 2024 17:30stdspstdspCPPg++9.4.0Success118,517
Feb 16, 2024 17:29stdspstdspCPPg++9.4.0Success118,588
Feb 16, 2024 17:29stdspstdspCPPg++9.4.0Success116,523
Feb 16, 2024 17:24stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:22stdspstdspCPPg++9.4.0Error
Feb 16, 2024 17:15parastromparastromRUSTrust-1.74.0Error
Feb 16, 2024 17:14parastromparastromRUSTrust-1.74.0Success133,211+31.64 RP
Feb 16, 2024 16:39stdspstdspCPPg++9.4.0Error
Feb 16, 2024 13:41Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 16, 2024 13:41Tejas G.Tejas G.CPPclang++10.0.0Error
Feb 16, 2024 10:05Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 08:29__RUSTrust-1.76.0Error
Feb 16, 2024 08:02tickracertickracerCPPg++9.4.0Success2,239,621+4.47 RP
Feb 16, 2024 07:33Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 07:32Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 07:31Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 06:37Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 06:36Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 06:16Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 06:14Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:54Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:53Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:49Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:49Tejas G.Tejas G.CPPg++9.4.0Success45,830+15.81 RP
Feb 16, 2024 05:47Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:47Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:47Tejas G.Tejas G.CPPg++9.4.0Success53,688
Feb 16, 2024 05:45Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 05:44Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:24Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:17Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:16Tejas G.Tejas G.CPPg++9.4.0Success60,784
Feb 16, 2024 03:06Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:06Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:05Tejas G.Tejas G.CPPg++9.4.0Success49,409+23.94 RP
Feb 16, 2024 03:01Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 03:00Tejas G.Tejas G.CPPg++9.4.0Error
Feb 15, 2024 20:250xUBN0xUBNRUSTrust-1.76.0Error
Feb 15, 2024 20:240xUBN0xUBNRUSTrust-1.76.0Error
Feb 15, 2024 20:11Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,111,914+0.12 RP
Feb 15, 2024 20:03Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,127,357+0.98 RP
Feb 15, 2024 20:01Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,867,866
Feb 15, 2024 19:57Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:54Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:49Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:48Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:41Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,267,096+0.05 RP
Feb 15, 2024 19:40Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:36Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:32Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:26Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Success1,274,388+2.60 RP
Feb 15, 2024 19:23Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:14Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:12Ryan Chung Yi ShengRyan Chung Yi ShengCPPg++9.4.0Error
Feb 15, 2024 19:09__RUSTrust-1.76.0Error