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 11, 2024 23:27yenw0dyenw0dGOgo1.19.3Success186,936
Feb 11, 2024 04:21Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 20:42yenw0dyenw0dGOgo1.19.3Success152,183
Feb 10, 2024 20:38yenw0dyenw0dGOgo1.19.3Error
Feb 10, 2024 20:30yenw0dyenw0dGOgo1.19.3Error
Feb 10, 2024 20:21yenw0dyenw0dGOgo1.21.3Error
Feb 10, 2024 20:14yenw0dyenw0dGOgo1.21.3Error
Feb 10, 2024 16:040xkake0xkakeRUSTrust-1.75.0Success115,181
Feb 10, 2024 16:030xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 16:030xkake0xkakeRUSTrust-1.75.0Success135,542
Feb 10, 2024 15:550xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 15:540xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 15:530xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 15:520xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 15:420xkake0xkakeRUSTrust-1.75.0Success112,076+0.02 RP
Feb 10, 2024 15:360xkake0xkakeRUSTrust-1.75.0Success112,097+26.19 RP
Feb 10, 2024 15:330xkake0xkakeRUSTrust-1.75.0Success158,682+0.44 RP
Feb 10, 2024 15:320xkake0xkakeRUSTrust-1.75.0Error
Feb 10, 2024 14:43YOM85tvE/1vsYOM85tvE/1vsCPPg++9.4.0Success2,240,066+4.46 RP
Feb 10, 2024 14:12Tejas G.Tejas G.CPPg++9.4.0Success57,638
Feb 10, 2024 14:12Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 14:11Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 14:11Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 14:10Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 13:54Tejas G.Tejas G.CPPg++9.4.0Success56,037+91.73 RP
Feb 10, 2024 13:45Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 13:11Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 12:43Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 12:42Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 12:41Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 07:06Zhiyang LuZhiyang LuCPPg++9.4.0Success2,249,809
Feb 10, 2024 07:02Zhiyang LuZhiyang LuCPPg++9.4.0Success2,413,163
Feb 10, 2024 06:41Zhiyang LuZhiyang LuCPPg++9.4.0Success2,240,490+4.46 RP
Feb 10, 2024 06:34Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 06:33Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 05:08Tejas G.Tejas G.CPPclang++10.0.0Success189,325
Feb 10, 2024 04:34Tejas G.Tejas G.CPPg++9.4.0Success115,312+32.22 RP
Feb 10, 2024 04:12Tejas G.Tejas G.CPPg++9.4.0Error
Feb 10, 2024 02:28Tejas G.Tejas G.CPPclang++10.0.0Success259,378
Feb 10, 2024 02:28Tejas G.Tejas G.CPPg++9.4.0Success183,493+0.04 RP
Feb 9, 2024 22:150xkake0xkakeRUSTrust-1.75.0Success159,793+2.73 RP
Feb 7, 2024 23:460xkake0xkakeRUSTrust-1.75.0Success167,214
Feb 7, 2024 23:450xkake0xkakeRUSTrust-1.75.0Success169,082
Feb 7, 2024 01:52korey-skorey-sRUSTrust-1.75.0Success678,626
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