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 18, 2024 04:48Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:45Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:41Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:40Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:36Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:32Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:30Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:28Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:28Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:24Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:22Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:22Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:16Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:12Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:12Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 03:36Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 03:31Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 00:40Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 00:40Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 17, 2024 15:28parastromparastromRUSTrust-1.74.0Success93,031+32.42 RP
Feb 17, 2024 10:35Marcus LeeMarcus LeeCPPclang++10.0.0Success2,330,804+4.29 RP
Feb 17, 2024 06:57matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:54matsuoka-601matsuoka-601CPPclang++10.0.0Success33,036+92.01 RP
Feb 17, 2024 06:54matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:52matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:52matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:49matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:48matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:47matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:44matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:44matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:42matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:20matsuoka-601matsuoka-601CPPg++9.4.0Error
Feb 17, 2024 06:19matsuoka-601matsuoka-601CPPg++9.4.0Error
Feb 17, 2024 06:19matsuoka-601matsuoka-601CPPg++9.4.0Success2,242,919
Feb 17, 2024 00:53Tejas G.Tejas G.CPPg++9.4.0Error
Feb 17, 2024 00:52Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 21:09Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 21:08Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 21:07Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success67,140
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,216
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,188
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success81,449
Feb 16, 2024 21:05Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,439
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