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 13, 2024 09:48Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:47Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:45Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:44Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:42Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:39Tejas G.Tejas G.CPPg++9.4.0Error
Feb 13, 2024 09:37Tejas G.Tejas G.CPPg++9.4.0Error
Feb 12, 2024 20:44__RUSTrust-1.74.0Error
Feb 12, 2024 20:43__RUSTrust-1.74.0Error
Feb 12, 2024 20:33__RUSTrust-1.74.0Error
Feb 12, 2024 20:33__RUSTrust-1.74.0Error
Feb 12, 2024 20:31__RUSTrust-1.74.0Error
Feb 12, 2024 20:30__RUSTrust-1.74.0Error
Feb 12, 2024 20:27__RUSTrust-1.74.0Success61,922+33.02 RP
Feb 12, 2024 20:26__RUSTrust-1.74.0Error
Feb 12, 2024 20:25__RUSTrust-1.74.0Error
Feb 12, 2024 20:21__RUSTrust-1.74.0Error
Feb 12, 2024 20:20__RUSTrust-1.74.0Error
Feb 12, 2024 20:18__RUSTrust-1.74.0Error
Feb 12, 2024 16:52yenw0dyenw0dGOgo1.21.7Success275,780
Feb 12, 2024 16:49yenw0dyenw0dGOgo1.21.7Success268,049
Feb 12, 2024 16:41yenw0dyenw0dGOgo1.21.7Success272,918
Feb 12, 2024 16:40yenw0dyenw0dGOgo1.21.7Success274,171
Feb 12, 2024 16:36yenw0dyenw0dGOgo1.21.7Success275,455
Feb 12, 2024 16:34Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:33Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:32Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success38,323
Feb 12, 2024 16:31Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:29yenw0dyenw0dGOgo1.21.7Error
Feb 12, 2024 16:29Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:28Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success37,690+14.96 RP
Feb 12, 2024 16:28Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:27Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:26Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success40,125
Feb 12, 2024 16:24Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:19Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 12, 2024 16:19Yuriy LyfenkoYuriy LyfenkoCPPclang++10.0.0Success46,827
Feb 12, 2024 16:18Yuriy LyfenkoYuriy LyfenkoCPPclang++10.0.0Error
Feb 12, 2024 12:58GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 12:22GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 12:21Tejas G.Tejas G.CPPg++9.4.0Error
Feb 12, 2024 12:20GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 12:19Tejas G.Tejas G.CPPg++9.4.0Error
Feb 12, 2024 12:18Tejas G.Tejas G.CPPg++9.4.0Error
Feb 12, 2024 12:16Tejas G.Tejas G.CPPg++9.4.0Error
Feb 12, 2024 12:01GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 11:15GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 11:08GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 11:04GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 11:00GuestGuestRUSTrust-1.76.0Error
Feb 12, 2024 03:37yenw0dyenw0dGOgo1.21.7Error
Feb 12, 2024 03:36yenw0dyenw0dGOgo1.21.7Error
Feb 12, 2024 03:29yenw0dyenw0dGOgo1.21.7Error
Feb 11, 2024 23:41yenw0dyenw0dGOgo1.21.7Success273,416
Feb 11, 2024 23:29yenw0dyenw0dGOgo1.19.3Success593,798
Feb 11, 2024 23:28yenw0dyenw0dGOgo1.19.3Success630,312
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