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 14, 2024 14:33__RUSTrust-1.74.0Error
Feb 14, 2024 01:23stdspstdspRUSTrust-1.76.0Success111,154
Feb 14, 2024 01:22stdspstdspRUSTrust-1.76.0Error
Feb 14, 2024 01:21stdspstdspRUSTrust-1.76.0Success112,279
Feb 14, 2024 01:19stdspstdspRUSTrust-1.76.0Success110,622+0.25 RP
Feb 14, 2024 01:16stdspstdspRUSTrust-1.76.0Success114,788
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,690
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,697
Feb 14, 2024 01:09stdspstdspRUSTrust-1.76.0Success112,672
Feb 14, 2024 01:08stdspstdspRUSTrust-1.76.0Success379,183
Feb 14, 2024 01:01stdspstdspRUSTrust-1.76.0Success111,567
Feb 14, 2024 00:20stdspstdspRUSTrust-1.76.0Success114,397
Feb 13, 2024 23:51stdspstdspRUSTrust-1.76.0Success271,357
Feb 13, 2024 23:46stdspstdspRUSTrust-1.76.0Success112,131
Feb 13, 2024 23:45stdspstdspRUSTrust-1.76.0Success112,554
Feb 13, 2024 23:44stdspstdspRUSTrust-1.76.0Success114,333
Feb 13, 2024 23:38stdspstdspRUSTrust-1.76.0Success111,935
Feb 13, 2024 23:33stdspstdspRUSTrust-1.76.0Success112,588
Feb 13, 2024 23:31stdspstdspRUSTrust-1.76.0Success110,932+0.68 RP
Feb 13, 2024 23:30stdspstdspRUSTrust-1.76.0Success114,905
Feb 13, 2024 23:29stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:26stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:24stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:16stdspstdspRUSTrust-1.76.0Success111,780+0.64 RP
Feb 13, 2024 23:15stdspstdspRUSTrust-1.76.0Success112,580+4.71 RP
Feb 13, 2024 23:14stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:11stdspstdspRUSTrust-1.76.0Error
Feb 13, 2024 23:05stdspstdspRUSTrust-1.76.0Success120,870
Feb 13, 2024 23:01stdspstdspRUSTrust-1.76.0Success128,010
Feb 13, 2024 23:00stdspstdspRUSTrust-1.76.0Success119,980
Feb 13, 2024 22:59stdspstdspRUSTrust-1.75.0Error
Feb 13, 2024 22:09stdspstdspRUSTrust-1.75.0Success120,512
Feb 13, 2024 22:00stdspstdspRUSTrust-1.75.0Success118,883+0.32 RP
Feb 13, 2024 21:58stdspstdspRUSTrust-1.75.0Success121,476
Feb 13, 2024 20:10__RUSTrust-1.74.0Error
Feb 13, 2024 20:10__RUSTrust-1.74.0Error
Feb 13, 2024 20:08__RUSTrust-1.74.0Error
Feb 13, 2024 20:08__RUSTrust-1.74.0Error
Feb 13, 2024 20:07__RUSTrust-1.74.0Error
Feb 13, 2024 19:49__RUSTrust-1.74.0Error
Feb 13, 2024 19:47__RUSTrust-1.74.0Error
Feb 13, 2024 09:57__RUSTrust-1.74.0Error
Feb 13, 2024 09:52__RUSTrust-1.74.0Error
Feb 13, 2024 09:51__RUSTrust-1.74.0Error
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