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 7, 2024 23:16stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:14stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:11stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:02stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:01stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:58stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:54stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:53stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:47stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:45stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:42stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:25stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:24stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:20stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:19stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:14stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:43stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:41stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:39stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:37stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:32stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:28stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:16stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:08stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:06stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:02stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:01stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:00stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:59stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:37stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:24stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:19stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 18:30stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 18:04stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:54stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:17stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:11stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:08stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:06stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:03stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 16:32stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 16:21stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:57stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:53stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:34stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:23stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:20stdspstdspRUSTrust-1.75.0Success161,378
Jan 7, 2024 15:18stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:07stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:03stdspstdspRUSTrust-1.75.0Error
Jan 3, 2024 22:35Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success90,336+13.60 RP
Jan 3, 2024 22:19Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Error
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