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
Mar 12, 2026 20:27pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:26pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:25pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:23pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:10pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:09pseppsepCPPg++13.3.0Success111,171+11.43 RP
Mar 12, 2026 20:07pseppsepCPPg++13.3.0Success577,312
Mar 12, 2026 20:06pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:03pseppsepCPPg++13.3.0Success127,355+1.22 RP
Mar 12, 2026 20:00pseppsepCPPg++13.3.0Success163,036
Mar 12, 2026 19:58pseppsepCPPg++13.3.0Success323,233
Mar 12, 2026 19:54pseppsepCPPg++13.3.0Success181,617
Mar 12, 2026 19:52pseppsepCPPg++13.3.0Success129,366+17.02 RP
Mar 12, 2026 19:50pseppsepCPPg++13.3.0Success165,905+33.53 RP
Mar 12, 2026 19:47pseppsepCPPg++13.3.0Success43,755,553
Mar 12, 2026 19:36pseppsepCPPg++13.3.0Success373,847+26.75 RP
Mar 12, 2026 00:16KeefeKeefeCPPclang++18.1.3Success7,941
Mar 11, 2026 23:55KeefeKeefeCPPg++13.3.0Success6,543+1'390.86 RP
Mar 11, 2026 18:33KeefeKeefeCPPclang++18.1.3Success89,883
Mar 11, 2026 18:32KeefeKeefeCPPg++13.3.0Success78,078
Mar 11, 2026 17:46KeefeKeefeCPPg++13.3.0Error
Mar 11, 2026 17:07KeefeKeefeCPPg++13.3.0Error
Mar 11, 2026 16:58KeefeKeefeCPPg++13.3.0Success121,555
Mar 10, 2026 22:28Josu San MartinJosu San MartinCPPg++13.3.0Success3,810
Mar 10, 2026 22:27Josu San MartinJosu San MartinCPPg++13.3.0Success3,952
Mar 10, 2026 22:25Josu San MartinJosu San MartinCPPg++13.3.0Success4,367
Mar 10, 2026 20:01Josu San MartinJosu San MartinRUSTrust-1.92.0Success4,457
Mar 10, 2026 19:42Josu San MartinJosu San MartinRUSTrust-1.92.0Success4,348
Mar 10, 2026 19:22Josu San MartinJosu San MartinCPPg++13.3.0Success4,114
Mar 10, 2026 18:38Josu San MartinJosu San MartinCPPg++13.3.0Success4,245
Mar 10, 2026 17:45Josu San MartinJosu San MartinCPPg++13.3.0Success3,800
Mar 10, 2026 17:44Josu San MartinJosu San MartinCPPg++13.3.0Success3,928
Mar 10, 2026 17:43Josu San MartinJosu San MartinCPPg++13.3.0Error
Mar 5, 2026 23:08Sergey SerebryakovSergey SerebryakovCPPg++13.3.0Success35,679+280.28 RP
Mar 3, 2026 01:00JuneLJuneLRUSTrust-1.93.1Success59,193
Mar 3, 2026 00:52JuneLJuneLRUSTrust-1.93.1Error
Mar 3, 2026 00:49JuneLJuneLRUSTrust-1.93.1Success46,048
Mar 3, 2026 00:40JuneLJuneLRUSTrust-1.93.1Success44,676+23.52 RP
Mar 3, 2026 00:35JuneLJuneLRUSTrust-1.93.1Success49,921+12.80 RP
Mar 3, 2026 00:35JuneLJuneLRUSTrust-1.93.1Success54,259
Mar 3, 2026 00:34JuneLJuneLRUSTrust-1.93.1Success58,083
Mar 2, 2026 22:25JuneLJuneLRUSTrust-1.93.1Error
Mar 2, 2026 22:23JuneLJuneLRUSTrust-1.93.1Success55,962
Mar 2, 2026 22:20JuneLJuneLRUSTrust-1.93.1Success146,562
Mar 2, 2026 22:16JuneLJuneLRUSTrust-1.93.1Success53,328+2.16 RP
Mar 2, 2026 22:12JuneLJuneLRUSTrust-1.93.1Success53,948+11.29 RP
Mar 2, 2026 22:08JuneLJuneLRUSTrust-1.93.1Success58,729
Mar 2, 2026 22:03JuneLJuneLRUSTrust-1.93.1Success57,448
Mar 2, 2026 21:56JuneLJuneLRUSTrust-1.93.1Success57,447+0.06 RP
Mar 2, 2026 21:54JuneLJuneLRUSTrust-1.93.1Success58,436
Mar 2, 2026 21:50JuneLJuneLRUSTrust-1.93.1Success83,997
Mar 2, 2026 21:45JuneLJuneLRUSTrust-1.93.1Success57,466+32.58 RP
Mar 2, 2026 21:41JuneLJuneLRUSTrust-1.93.1Success70,703+4.87 RP
Mar 2, 2026 21:39JuneLJuneLRUSTrust-1.93.1Error
Mar 2, 2026 21:35JuneLJuneLRUSTrust-1.93.1Success86,003
Mar 2, 2026 21:28JuneLJuneLRUSTrust-1.93.1Success106,303
Mar 2, 2026 21:27JuneLJuneLRUSTrust-1.93.1Error
Mar 2, 2026 21:26JuneLJuneLRUSTrust-1.93.1Success73,224+7.31 RP
Mar 2, 2026 21:22JuneLJuneLRUSTrust-1.93.1Success77,364+17.79 RP
Mar 2, 2026 21:16JuneLJuneLRUSTrust-1.93.1Success100,229
Mar 2, 2026 20:58JuneLJuneLRUSTrust-1.93.1Success89,709+4.56 RP
Mar 2, 2026 20:49JuneLJuneLRUSTrust-1.93.1Success93,531+3.05 RP
Mar 2, 2026 20:46JuneLJuneLRUSTrust-1.93.1Error
Mar 2, 2026 20:40JuneLJuneLRUSTrust-1.93.1Success125,257
Mar 2, 2026 20:35JuneLJuneLRUSTrust-1.93.1Success96,281+16.42 RP
Mar 2, 2026 20:31JuneLJuneLRUSTrust-1.93.1Success114,355+87.45 RP
Mar 2, 2026 09:460xZohan0xZohanRUSTrust-1.93.1Success6,183
Mar 2, 2026 09:460xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:460xZohan0xZohanRUSTrust-1.93.1Success13,245
Mar 2, 2026 09:440xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:440xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:440xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:430xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:430xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:420xZohan0xZohanRUSTrust-1.93.1Error+116.33 RP
Mar 2, 2026 09:420xZohan0xZohanRUSTrust-1.93.1Success6,395
Mar 2, 2026 09:420xZohan0xZohanRUSTrust-1.93.1Success6,595
Mar 2, 2026 09:420xZohan0xZohanRUSTrust-1.93.1Success6,291+11.29 RP
Mar 2, 2026 09:400xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:400xZohan0xZohanRUSTrust-1.93.1Success6,336+385.39 RP
Mar 2, 2026 09:400xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:390xZohan0xZohanRUSTrust-1.93.1Success8,383+432.84 RP
Mar 2, 2026 09:390xZohan0xZohanRUSTrust-1.93.1Success13,157+613.85 RP
Mar 2, 2026 09:370xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 09:360xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 08:540xZohan0xZohanRUSTrust-1.93.1Success83,329
Mar 2, 2026 07:550xZohan0xZohanRUSTrust-1.93.1Error
Mar 2, 2026 07:350xZohan0xZohanRUSTrust-1.93.1Success70,719
Mar 1, 2026 22:590xZohan0xZohanRUSTrust-1.93.1Success71,107
Mar 1, 2026 21:500xZohan0xZohanRUSTrust-1.93.1Success71,459
Mar 1, 2026 21:400xZohan0xZohanRUSTrust-1.93.1Success79,145
Mar 1, 2026 21:290xZohan0xZohanRUSTrust-1.93.1Success103,224
Mar 1, 2026 20:310xZohan0xZohanRUSTrust-1.93.1Success102,995
Mar 1, 2026 20:300xZohan0xZohanRUSTrust-1.93.1Success81,057
Mar 1, 2026 16:110xZohan0xZohanRUSTrust-1.93.1Success69,105
Mar 1, 2026 16:100xZohan0xZohanRUSTrust-1.93.1Error
Mar 1, 2026 15:390xZohan0xZohanRUSTrust-1.93.1Success89,984
Mar 1, 2026 15:140xZohan0xZohanRUSTrust-1.93.1Success94,945
Mar 1, 2026 15:040xZohan0xZohanRUSTrust-1.93.1Success73,410
Mar 1, 2026 14:360xZohan0xZohanRUSTrust-1.93.1Success68,400+0.84 RP