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
Feb 11, 2026 11:38Code HintsCode HintsRUSTrust-1.93.0Success553,943
Feb 11, 2026 11:33Code HintsCode HintsRUSTrust-1.93.0Success194,572
Feb 11, 2026 11:31Code HintsCode HintsRUSTrust-1.93.0Success60,179
Feb 11, 2026 11:30Code HintsCode HintsRUSTrust-1.93.0Success64,816
Feb 11, 2026 11:29Code HintsCode HintsRUSTrust-1.93.0Success53,631
Feb 11, 2026 11:28Code HintsCode HintsRUSTrust-1.93.0Success65,997
Feb 10, 2026 22:58Code HintsCode HintsRUSTrust-1.93.0Success63,348
Feb 10, 2026 22:57Code HintsCode HintsRUSTrust-1.93.0Success63,945
Feb 10, 2026 19:15undenuicapundenuicapRUSTrust-1.93.0Success43,410+230.36 RP
Feb 10, 2026 18:51Code HintsCode HintsRUSTrust-1.93.0Success59,466
Feb 10, 2026 18:22Code HintsCode HintsRUSTrust-1.93.0Success46,791+68.88 RP
Feb 10, 2026 18:19Code HintsCode HintsRUSTrust-1.93.0Success81,153
Feb 10, 2026 18:11Code HintsCode HintsRUSTrust-1.93.0Success70,497
Feb 10, 2026 18:06Code HintsCode HintsRUSTrust-1.93.0Success69,045+19.43 RP
Feb 10, 2026 17:52Code HintsCode HintsRUSTrust-1.93.0Success162,281
Feb 10, 2026 17:48Code HintsCode HintsRUSTrust-1.93.0Success430,547
Feb 10, 2026 17:40Code HintsCode HintsRUSTrust-1.93.0Success575,740
Feb 10, 2026 17:31Code HintsCode HintsRUSTrust-1.93.0Success87,193
Feb 10, 2026 17:15Code HintsCode HintsRUSTrust-1.93.0Success116,364
Feb 10, 2026 17:11Code HintsCode HintsRUSTrust-1.93.0Success99,250
Feb 10, 2026 17:01Code HintsCode HintsRUSTrust-1.93.0Success80,064
Feb 10, 2026 16:54Code HintsCode HintsRUSTrust-1.93.0Success677,288
Feb 10, 2026 16:49Code HintsCode HintsRUSTrust-1.93.0Success81,417
Feb 10, 2026 16:47Code HintsCode HintsRUSTrust-1.93.0Success97,157
Feb 10, 2026 16:19Code HintsCode HintsRUSTrust-1.93.0Error
Feb 10, 2026 16:07Code HintsCode HintsRUSTrust-1.93.0Success82,998
Feb 10, 2026 15:46Code HintsCode HintsRUSTrust-1.93.0Success403,117
Feb 10, 2026 15:35Code HintsCode HintsRUSTrust-1.93.0Success424,819
Feb 10, 2026 15:19Code HintsCode HintsRUSTrust-1.93.0Success158,345
Feb 10, 2026 15:14Code HintsCode HintsRUSTrust-1.93.0Success110,153
Feb 10, 2026 15:11Code HintsCode HintsRUSTrust-1.93.0Success80,845
Feb 10, 2026 15:00Code HintsCode HintsRUSTrust-1.93.0Success80,145
Feb 10, 2026 14:42Code HintsCode HintsRUSTrust-1.93.0Success83,962
Feb 10, 2026 14:39Code HintsCode HintsRUSTrust-1.93.0Success81,907
Feb 10, 2026 14:15Code HintsCode HintsRUSTrust-1.93.0Success79,976
Feb 10, 2026 13:39Code HintsCode HintsRUSTrust-1.93.0Success184,547
Feb 10, 2026 13:35Code HintsCode HintsRUSTrust-1.93.0Success80,474
Feb 10, 2026 13:04Code HintsCode HintsRUSTrust-1.93.0Success105,478
Feb 10, 2026 13:00Code HintsCode HintsRUSTrust-1.93.0Success79,745+0.87 RP
Feb 9, 2026 23:42Code HintsCode HintsRUSTrust-1.93.0Success80,302+0.17 RP
Feb 9, 2026 23:42Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:41Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:40Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:38Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:36Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:34Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:33Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:31Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:30Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:23Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:23Code HintsCode HintsRUSTrust-1.93.0Success149,771
Feb 9, 2026 23:22Code HintsCode HintsRUSTrust-1.93.0Success173,069
Feb 9, 2026 23:21Code HintsCode HintsRUSTrust-1.93.0Success317,167
Feb 9, 2026 23:19Code HintsCode HintsRUSTrust-1.93.0Success243,448
Feb 9, 2026 23:18Code HintsCode HintsRUSTrust-1.93.0Success209,664
Feb 9, 2026 23:17Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:16Code HintsCode HintsRUSTrust-1.93.0Success152,948
Feb 9, 2026 23:14Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:12Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:11Code HintsCode HintsRUSTrust-1.93.0Success170,207
Feb 9, 2026 23:10Code HintsCode HintsRUSTrust-1.93.0Success187,417
Feb 9, 2026 23:09Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:08Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:07Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:07Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 23:05Code HintsCode HintsRUSTrust-1.93.0Success91,624
Feb 9, 2026 23:01Code HintsCode HintsRUSTrust-1.93.0Success119,893
Feb 9, 2026 23:01Code HintsCode HintsRUSTrust-1.93.0Success80,534
Feb 9, 2026 23:00Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 22:55Code HintsCode HintsRUSTrust-1.93.0Success80,409+0.11 RP
Feb 9, 2026 22:50Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 22:50Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 22:49Code HintsCode HintsRUSTrust-1.93.0Success117,028
Feb 9, 2026 22:46Code HintsCode HintsRUSTrust-1.93.0Success80,483+15.64 RP
Feb 9, 2026 22:45Code HintsCode HintsRUSTrust-1.93.0Error
Feb 9, 2026 22:06Code HintsCode HintsRUSTrust-1.93.0Success93,140
Feb 9, 2026 22:00Code HintsCode HintsRUSTrust-1.93.0Success92,905
Feb 9, 2026 22:00Code HintsCode HintsRUSTrust-1.85.1Success93,952
Feb 8, 2026 19:49Tim ZhamoidinTim ZhamoidinCPPclang++18.1.3Error
Feb 8, 2026 13:14Daniel MastalerzDaniel MastalerzCPPg++13.3.0Success108,610+6.84 RP
Feb 8, 2026 13:11Daniel MastalerzDaniel MastalerzCPPg++13.3.0Success117,328+28.65 RP
Feb 8, 2026 13:10Daniel MastalerzDaniel MastalerzCPPg++13.3.0Success243,079
Feb 8, 2026 13:09Daniel MastalerzDaniel MastalerzCPPg++13.3.0Success2,007,876
Feb 8, 2026 13:08Daniel MastalerzDaniel MastalerzCPPg++13.3.0Success176,722+56.59 RP
Feb 8, 2026 13:04Daniel MastalerzDaniel MastalerzCPPg++13.3.0Error
Feb 7, 2026 15:43lipapipalipapipaCPPg++13.3.0Success4,745+1'373.37 RP
Feb 7, 2026 09:33lipapipalipapipaCPPg++13.3.0Error
Feb 7, 2026 08:43lipapipalipapipaCPPg++13.3.0Success13,622+537.03 RP
Feb 7, 2026 00:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 7, 2026 00:08Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 6, 2026 23:55Josu San MartinJosu San MartinCPPg++13.3.0Success5,810+4.14 RP
Feb 6, 2026 23:47Josu San MartinJosu San MartinCPPg++13.3.0Error
Feb 6, 2026 23:44Josu San MartinJosu San MartinCPPclang++18.1.3Success6,948
Feb 6, 2026 23:43Josu San MartinJosu San MartinCPPg++13.3.0Success5,824+11.13 RP
Feb 6, 2026 23:29lipapipalipapipaCPPg++13.3.0Success50,741+16.85 RP
Feb 6, 2026 22:27lipapipalipapipaCPPg++13.3.0Success55,484+36.64 RP
Feb 6, 2026 22:04lipapipalipapipaCPPg++13.3.0Success73,171
Feb 6, 2026 22:03lipapipalipapipaCPPg++13.3.0Success69,640+11.84 RP
Feb 6, 2026 22:01lipapipalipapipaCPPg++13.3.0Error
Feb 6, 2026 20:45lipapipalipapipaCPPg++13.3.0Success91,164