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
Nov 28, 2022 02:54apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 02:53apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 02:52apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 02:06apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 02:06apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:55apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:52apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:50apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:33apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:32apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:29apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:26apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:25apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:24apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:24apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:21apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:20apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:15apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 01:09apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 00:15apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 00:15apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 00:15apuxbtapuxbtCPPg++9.3.0Success2,250,758+0.01 RP
Nov 28, 2022 00:14apuxbtapuxbtCPPg++9.3.0Success2,361,036
Nov 28, 2022 00:14apuxbtapuxbtCPPg++9.3.0Error
Nov 28, 2022 00:13apuxbtapuxbtCPPg++9.3.0Error
Nov 27, 2022 19:05apuxbtapuxbtCPPg++9.3.0Error
Nov 27, 2022 16:38apuxbtapuxbtCPPg++9.3.0Success2,265,870
Nov 27, 2022 16:38apuxbtapuxbtCPPg++9.3.0Success2,315,645
Nov 27, 2022 16:37apuxbtapuxbtCPPg++9.3.0Success2,259,790
Nov 27, 2022 16:37apuxbtapuxbtCPPg++9.3.0Success2,254,459+0.01 RP
Nov 27, 2022 16:36apuxbtapuxbtCPPg++9.3.0Error
Nov 27, 2022 16:36apuxbtapuxbtCPPg++9.3.0Success2,259,822+4.43 RP
Nov 27, 2022 16:35apuxbtapuxbtCPPg++9.3.0Error
Oct 13, 2022 19:35Kenneth MaplesKenneth MaplesCPPg++9.3.0Success97,110+2.07 RP
Oct 13, 2022 19:31Kenneth MaplesKenneth MaplesCPPg++9.3.0Success99,102+7.41 RP
Oct 13, 2022 19:31Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 19:30Kenneth MaplesKenneth MaplesCPPg++9.3.0Success106,959+19.10 RP
Oct 13, 2022 19:29Kenneth MaplesKenneth MaplesCPPg++9.3.0Success134,428+43.06 RP
Oct 13, 2022 19:19Kenneth MaplesKenneth MaplesCPPg++9.3.0Success323,514
Oct 13, 2022 19:18Kenneth MaplesKenneth MaplesCPPg++9.3.0Success319,166+0.56 RP
Oct 13, 2022 19:17Kenneth MaplesKenneth MaplesCPPg++9.3.0Success325,011+1.32 RP
Oct 13, 2022 19:15Kenneth MaplesKenneth MaplesCPPg++9.3.0Success339,539+18.13 RP
Oct 13, 2022 19:09Kenneth MaplesKenneth MaplesCPPg++9.3.0Success883,002+0.49 RP
Oct 13, 2022 19:06Kenneth MaplesKenneth MaplesCPPg++9.3.0Success924,979
Oct 13, 2022 19:05Kenneth MaplesKenneth MaplesCPPg++9.3.0Success922,910+0.32 RP
Oct 13, 2022 19:02Kenneth MaplesKenneth MaplesCPPg++9.3.0Success951,112+4.95 RP
Oct 13, 2022 19:02Kenneth MaplesKenneth MaplesCPPg++9.3.0Success3,202,817
Oct 13, 2022 19:00Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 18:10Kenneth MaplesKenneth MaplesCPPg++9.3.0Success3,200,005
Oct 13, 2022 18:09Kenneth MaplesKenneth MaplesCPPg++9.3.0Success3,680,510
Oct 13, 2022 18:03Kenneth MaplesKenneth MaplesCPPg++9.3.0Success3,784,894
Oct 13, 2022 17:59Kenneth MaplesKenneth MaplesCPPg++9.3.0Success4,806,796
Oct 13, 2022 17:57Kenneth MaplesKenneth MaplesCPPg++9.3.0Success4,775,215
Oct 13, 2022 17:34Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
Oct 13, 2022 17:32Kenneth MaplesKenneth MaplesCPPg++9.3.0Error
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