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
Oct 13, 2022 17:31Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:30Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:28Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:26Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:25Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:21Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 16:59Kenneth MaplesKenneth MaplesCPPg++9.3.0Success1,796,908+1.35 RP
Oct 13, 2022 16:53Kenneth MaplesKenneth MaplesCPPg++9.3.0Success2,374,240+4.21 RP
Oct 13, 2022 16:47Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 16:39Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:18Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:15Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:13Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:13Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:11Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 12, 2022 02:06Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Sep 16, 2022 14:09fintarsfintarsCPPg++9.3.0Success2,261,587+4.42 RP
Sep 6, 2022 10:04unsatcoreunsatcoreCPPg++9.3.0Error
Sep 6, 2022 10:02unsatcoreunsatcoreCPPg++9.3.0Success107,926+3.00 RP
Sep 6, 2022 10:01unsatcoreunsatcoreCPPg++9.3.0Error
Sep 6, 2022 09:54unsatcoreunsatcoreCPPg++9.3.0Success132,635
Sep 6, 2022 09:53unsatcoreunsatcoreCPPg++9.3.0Error
Sep 6, 2022 09:53unsatcoreunsatcoreCPPg++9.3.0Error
Sep 1, 2022 19:08emily graceemily graceCPPg++9.3.0Success2,269,830+4.41 RP
Aug 29, 2022 11:56unsatcoreunsatcoreCPPg++9.3.0Error
Aug 29, 2022 11:56unsatcoreunsatcoreCPPg++9.3.0Error
Aug 29, 2022 11:51unsatcoreunsatcoreCPPg++9.3.0Success131,228
Aug 22, 2022 17:40Eugene ZhydzetskiEugene ZhydzetskiGOgo1.19Success1,407,656+7.10 RP
Aug 21, 2022 14:56unsatcoreunsatcoreCPPg++9.3.0Success131,468
Jul 21, 2022 17:06Robert BurkeRobert BurkeCPPg++9.3.0Success2,181,020+4.59 RP
Apr 27, 2022 13:27gchebanovgchebanovGOgo1.18.1Error
Apr 27, 2022 13:25gchebanovgchebanovGOgo1.18.1Success1,421,973
Apr 27, 2022 13:22gchebanovgchebanovGOgo1.18.1Success1,419,459+7.04 RP
Apr 27, 2022 13:19gchebanovgchebanovGOgo1.18.1Error
Feb 27, 2022 11:16Sergey StreminSergey StreminRUSTrust-1.59.0Success104,510
Feb 27, 2022 11:12Sergey StreminSergey StreminRUSTrust-1.59.0Success102,193
Feb 27, 2022 11:03Sergey StreminSergey StreminRUSTrust-1.59.0Success98,432
Feb 27, 2022 10:56Sergey StreminSergey StreminRUSTrust-1.59.0Success96,118
Feb 27, 2022 10:50Sergey StreminSergey StreminRUSTrust-1.59.0Success106,675
Feb 27, 2022 10:30Sergey StreminSergey StreminRUSTrust-1.59.0Success95,948+5.32 RP
Feb 27, 2022 10:29Sergey StreminSergey StreminRUSTrust-1.59.0Success193,934
Feb 27, 2022 10:29Sergey StreminSergey StreminRUSTrust-1.56.1Success207,573
Feb 27, 2022 10:14Sergey StreminSergey StreminRUSTrust-1.56.1Error
Feb 27, 2022 10:14Sergey StreminSergey StreminRUSTrust-1.56.1Error
Feb 27, 2022 10:12Sergey StreminSergey StreminRUSTrust-1.56.1Error
Feb 18, 2022 09:25Istomin MikhailIstomin MikhailCPPg++9.3.0Success69,260
Feb 18, 2022 09:24Istomin MikhailIstomin MikhailCPPg++9.3.0Success97,103
Feb 18, 2022 09:21Istomin MikhailIstomin MikhailCPPg++9.3.0Success88,959
Feb 18, 2022 09:20Istomin MikhailIstomin MikhailCPPg++9.3.0Success84,753
Feb 18, 2022 09:16Istomin MikhailIstomin MikhailCPPg++9.3.0Success69,881
Feb 18, 2022 09:11Istomin MikhailIstomin MikhailCPPg++9.3.0Success86,057
Feb 18, 2022 09:11Istomin MikhailIstomin MikhailCPPg++9.3.0Success86,395
Feb 18, 2022 09:09Istomin MikhailIstomin MikhailCPPg++9.3.0Success104,867
Feb 18, 2022 07:19Istomin MikhailIstomin MikhailCPPg++9.3.0Success71,266
Feb 18, 2022 06:50Istomin MikhailIstomin MikhailCPPg++9.3.0Success85,406
Feb 18, 2022 06:45Istomin MikhailIstomin MikhailCPPg++9.3.0Success96,904
Feb 18, 2022 06:39Istomin MikhailIstomin MikhailCPPg++9.3.0Success85,470
Feb 18, 2022 06:31Istomin MikhailIstomin MikhailCPPg++9.3.0Success86,379
Feb 18, 2022 06:05Istomin MikhailIstomin MikhailCPPg++9.3.0Success85,278
Feb 18, 2022 06:05Istomin MikhailIstomin MikhailCPPg++9.3.0Success86,299
Feb 18, 2022 06:04Istomin MikhailIstomin MikhailCPPg++9.3.0Success96,086
Feb 18, 2022 06:04Istomin MikhailIstomin MikhailCPPclang++10.0.0Success101,881
Feb 18, 2022 06:03Istomin MikhailIstomin MikhailCPPg++9.3.0Success115,004
Feb 18, 2022 05:55Istomin MikhailIstomin MikhailCPPg++9.3.0Success83,326
Feb 18, 2022 05:44Istomin MikhailIstomin MikhailCPPg++9.3.0Success66,474+30.86 RP
Feb 18, 2022 05:37Istomin MikhailIstomin MikhailCPPg++9.3.0Success83,628+4.70 RP
Feb 16, 2022 03:40Istomin MikhailIstomin MikhailCPPg++9.3.0Success87,047+13.70 RP
Feb 16, 2022 03:31Istomin MikhailIstomin MikhailCPPg++9.3.0Success98,833+29.27 RP
Feb 16, 2022 03:27Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 12, 2022 13:51Istomin MikhailIstomin MikhailCPPg++9.3.0Success139,068+0.09 RP
Feb 12, 2022 12:44Istomin MikhailIstomin MikhailCPPg++9.3.0Success1,776,835
Feb 12, 2022 09:50Istomin MikhailIstomin MikhailCPPg++9.3.0Success187,616
Feb 12, 2022 08:29Istomin MikhailIstomin MikhailCPPg++9.3.0Success155,653
Feb 12, 2022 08:15Istomin MikhailIstomin MikhailCPPg++9.3.0Success139,242+4.77 RP
Feb 11, 2022 19:10Istomin MikhailIstomin MikhailCPPg++9.3.0Success150,068
Feb 11, 2022 19:09Istomin MikhailIstomin MikhailCPPg++9.3.0Success152,478
Feb 11, 2022 19:06Istomin MikhailIstomin MikhailCPPg++9.3.0Success149,948
Feb 11, 2022 18:55Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 18:48Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 18:47Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 18:34Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 17:57Istomin MikhailIstomin MikhailCPPg++9.3.0Success150,836
Feb 11, 2022 17:27Istomin MikhailIstomin MikhailCPPg++9.3.0Success149,142+5.37 RP
Feb 11, 2022 16:13Istomin MikhailIstomin MikhailCPPg++9.3.0Success162,122+0.66 RP
Feb 11, 2022 15:54Istomin MikhailIstomin MikhailCPPg++9.3.0Success163,871+0.08 RP
Feb 11, 2022 15:46Istomin MikhailIstomin MikhailCPPg++9.3.0Success164,077+0.12 RP
Feb 11, 2022 15:38Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 15:33Istomin MikhailIstomin MikhailCPPg++9.3.0Success164,407+0.29 RP
Feb 11, 2022 15:32Istomin MikhailIstomin MikhailCPPg++9.3.0Success165,205+0.10 RP
Feb 11, 2022 15:32Istomin MikhailIstomin MikhailCPPg++9.3.0Success166,249
Feb 11, 2022 15:16Istomin MikhailIstomin MikhailCPPg++9.3.0Success212,321
Feb 11, 2022 15:15Istomin MikhailIstomin MikhailCPPg++9.3.0Success197,692
Feb 11, 2022 15:03Istomin MikhailIstomin MikhailCPPg++9.3.0Success172,846
Feb 11, 2022 14:56Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 11:48Istomin MikhailIstomin MikhailCPPg++9.3.0Success266,798
Feb 11, 2022 04:35Istomin MikhailIstomin MikhailCPPg++9.3.0Success196,619
Feb 11, 2022 04:35Istomin MikhailIstomin MikhailCPPg++9.3.0Error
Feb 11, 2022 04:19Istomin MikhailIstomin MikhailCPPg++9.3.0Success178,626
Feb 11, 2022 04:14Istomin MikhailIstomin MikhailCPPg++9.3.0Success165,489+2.48 RP
Feb 11, 2022 02:44Istomin MikhailIstomin MikhailCPPg++9.3.0Success172,563+0.36 RP