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
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
Dec 21, 2023 10:00DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 09:56DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 09:53DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 09:49DenisDenisRUSTrust-1.74.1Error
Dec 21, 2023 09:40DenisDenisRUSTrust-1.74.1Error
Dec 20, 2023 19:25Kenneth MaplesKenneth MaplesCPPg++9.3.0Success87,930+3.01 RP
Dec 20, 2023 15:24parastromparastromRUSTrust-1.74.0Error
Dec 20, 2023 15:20parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:28parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:27parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:26parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:24parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:14parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:14parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:09parastromparastromRUSTrust-1.74.0Error
Dec 18, 2023 00:08parastromparastromRUSTrust-1.74.0Error
Dec 17, 2023 06:20Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 17, 2023 06:08Kenneth MaplesKenneth MaplesCPPg++9.3.0Success96,311
Dec 17, 2023 06:07Kenneth MaplesKenneth MaplesCPPg++9.3.0Success92,390
Dec 17, 2023 06:04Kenneth MaplesKenneth MaplesCPPg++9.3.0Success96,905
Dec 17, 2023 06:03Kenneth MaplesKenneth MaplesCPPg++9.3.0Success93,286
Dec 17, 2023 05:59Kenneth MaplesKenneth MaplesCPPg++9.3.0Success98,162
Dec 17, 2023 05:53Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 17, 2023 05:45Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 17, 2023 05:31Kenneth MaplesKenneth MaplesCPPg++9.3.0Success96,831
Dec 17, 2023 05:28Kenneth MaplesKenneth MaplesCPPg++9.3.0Success94,298
Dec 17, 2023 05:26Kenneth MaplesKenneth MaplesCPPg++9.3.0Success93,210
Dec 17, 2023 05:21Kenneth MaplesKenneth MaplesCPPg++9.3.0Success94,905
Dec 17, 2023 05:20Kenneth MaplesKenneth MaplesCPPg++9.3.0Success90,322+0.20 RP
Dec 17, 2023 05:19Kenneth MaplesKenneth MaplesCPPg++9.3.0Success90,488+7.54 RP
Dec 17, 2023 05:18Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Dec 16, 2023 18:24mebowlermebowlerGOgo1.21.5Error
Dec 15, 2023 16:07parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 16:06parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 15:01parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:57parastromparastromRUSTrust-1.74.0Success230,247+33.03 RP
Dec 15, 2023 14:54parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:47parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:45parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:27parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:24parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:23parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:21parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:20parastromparastromRUSTrust-1.74.0Error
Dec 15, 2023 14:19matsuoka-601matsuoka-601CPPclang++10.0.0Error