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
Jan 3, 2024 22:07Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success104,036
Jan 3, 2024 22:06Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Error
Jan 3, 2024 22:06Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Error
Jan 3, 2024 22:02Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success105,058
Jan 3, 2024 21:52Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success103,800
Jan 3, 2024 21:51Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Error
Jan 3, 2024 21:48Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success102,992+0.01 RP
Jan 3, 2024 21:47Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success102,999+0.15 RP
Jan 3, 2024 21:47Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success103,156+0.69 RP
Jan 3, 2024 21:40Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success103,894+22.95 RP
Jan 3, 2024 21:38Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Error
Jan 3, 2024 21:35Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success137,720
Jan 3, 2024 21:06Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success136,424+0.75 RP
Jan 3, 2024 21:06Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success140,375
Jan 3, 2024 21:05Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success137,825+0.31 RP
Jan 3, 2024 20:54Yurkov AlekseyYurkov AlekseyCPPg++9.3.0Success138,416+66.49 RP
Jan 2, 2024 09:58stdspstdspRUSTrust-1.75.0Error
Jan 2, 2024 09:53stdspstdspRUSTrust-1.75.0Error
Jan 2, 2024 09:43stdspstdspRUSTrust-1.75.0Error
Jan 2, 2024 09:39stdspstdspRUSTrust-1.75.0Success159,305+0.33 RP
Jan 2, 2024 09:35stdspstdspRUSTrust-1.75.0Error
Jan 2, 2024 09:29stdspstdspRUSTrust-1.75.0Error
Jan 2, 2024 09:29stdspstdspRUSTrust-1.75.0Error
Dec 27, 2023 10:31Oleg KovalovOleg KovalovCPPclang++10.0.0Success2,272,058
Dec 25, 2023 16:55Yuriy LyfenkoYuriy LyfenkoCPPclang++10.0.0Success45,749
Dec 25, 2023 06:59stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 06:55stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 06:54stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 06:48stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 02:15Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 02:12Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 02:12Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 02:10Kenneth MaplesKenneth MaplesCPPg++9.3.0Success95,021
Dec 25, 2023 01:53Kenneth MaplesKenneth MaplesCPPg++9.3.0Success94,890
Dec 25, 2023 01:52stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:52Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 01:51Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 01:48Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 25, 2023 01:46stdspstdspRUSTrust-1.74.1Success161,718
Dec 25, 2023 01:46Kenneth MaplesKenneth MaplesCPPg++9.3.0Success88,858
Dec 25, 2023 01:45stdspstdspRUSTrust-1.74.1Success163,663
Dec 25, 2023 01:43Kenneth MaplesKenneth MaplesCPPg++9.3.0Success96,674
Dec 25, 2023 01:42stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:38stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:37stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:36Kenneth MaplesKenneth MaplesCPPg++9.3.0Success93,448
Dec 25, 2023 01:36Kenneth MaplesKenneth MaplesCPPg++9.3.0Success93,191
Dec 25, 2023 01:36stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:35stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:32stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:27stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:21stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 01:20stdspstdspRUSTrust-1.74.1Error
Dec 25, 2023 00:56stdspstdspRUSTrust-1.74.1Success223,993
Dec 25, 2023 00:54stdspstdspRUSTrust-1.74.1Success329,039
Dec 24, 2023 22:17stdspstdspRUSTrust-1.74.1Success160,197
Dec 24, 2023 22:15stdspstdspRUSTrust-1.74.1Success160,260
Dec 24, 2023 21:59stdspstdspRUSTrust-1.74.1Success160,154+0.09 RP
Dec 24, 2023 21:57stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:56stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:55stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:49stdspstdspRUSTrust-1.74.1Success26,236,100
Dec 24, 2023 21:47stdspstdspRUSTrust-1.74.1Success31,488,933
Dec 24, 2023 21:45stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:45stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:39stdspstdspRUSTrust-1.74.1Success27,272,831
Dec 24, 2023 21:37stdspstdspRUSTrust-1.74.1Success24,901,449
Dec 24, 2023 21:29stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:23stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:21stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:14stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:12stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:10stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:06stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 21:04stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 20:59stdspstdspRUSTrust-1.74.1Error
Dec 24, 2023 20:48stdspstdspRUSTrust-1.74.1Success160,379+0.03 RP
Dec 22, 2023 15:20mebowlermebowlerGOgo1.21.5Error
Dec 22, 2023 15:14mebowlermebowlerGOgo1.21.5Error
Dec 22, 2023 15:09mebowlermebowlerGOgo1.21.5Error
Dec 21, 2023 14:29DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 14:28DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 14:19DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 14:01DenisDenisRUSTrust-1.74.1Success9,059,472
Dec 21, 2023 13:58DenisDenisRUSTrust-1.74.1Success934,045
Dec 21, 2023 13:58DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 12:53DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 12:33DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 12:16DenisDenisRUSTrust-1.74.1Success905,169+0.25 RP
Dec 21, 2023 12:15DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 12:13DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 11:50DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 11:19DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 11:13DenisDenisRUSTrust-1.74.1Success926,154+4.10 RP
Dec 21, 2023 10:28DenisDenisRUSTrust-1.74.1Success1,505,556
Dec 21, 2023 10:27DenisDenisRUSTrust-1.74.1Success1,507,984
Dec 21, 2023 10:22DenisDenisRUSTrust-1.74.1Success1,507,549
Dec 21, 2023 10:18DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 10:07DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 10:04DenisDenisRUSTrust-1.74.1Success1,493,729+6.69 RP