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 6, 2026 20:45lipapipalipapipaCPPg++13.3.0Success91,164
Feb 6, 2026 20:44lipapipalipapipaCPPg++13.3.0Success75,898+1.92 RP
Feb 6, 2026 20:44lipapipalipapipaCPPg++13.3.0Success81,755
Feb 6, 2026 20:30lipapipalipapipaCPPg++13.3.0Success77,022+19.01 RP
Feb 6, 2026 20:20lipapipalipapipaCPPg++13.3.0Error
Feb 6, 2026 20:19lipapipalipapipaCPPg++13.3.0Error
Feb 6, 2026 20:19lipapipalipapipaCPPg++13.3.0Success90,233+89.16 RP
Feb 6, 2026 20:18lipapipalipapipaCPPg++13.3.0Error
Feb 6, 2026 20:08lipapipalipapipaCPPg++13.3.0Success461,538+21.67 RP
Feb 3, 2026 22:16olliecrowolliecrowCPPg++13.3.0Success95,378
Feb 3, 2026 22:16olliecrowolliecrowCPPg++13.3.0Success64,905
Feb 3, 2026 22:16olliecrowolliecrowCPPg++13.3.0Success60,493
Feb 3, 2026 22:16olliecrowolliecrowCPPg++13.3.0Success69,805
Feb 3, 2026 22:02olliecrowolliecrowCPPg++13.3.0Success89,159
Feb 3, 2026 21:48olliecrowolliecrowCPPg++13.3.0Success68,716
Feb 3, 2026 21:47olliecrowolliecrowCPPg++13.3.0Success69,243
Feb 3, 2026 21:47olliecrowolliecrowCPPg++13.3.0Success86,541
Feb 3, 2026 21:46olliecrowolliecrowCPPg++13.3.0Success67,359
Feb 2, 2026 05:27Zhou180Zhou180RUSTrust-1.93.0Success124,150+2.49 RP
Feb 2, 2026 05:07Zhou180Zhou180RUSTrust-1.93.0Success128,116+78.05 RP
Jan 30, 2026 17:11olliecrowolliecrowRUSTrust-1.93.0Success86,228
Jan 30, 2026 17:10olliecrowolliecrowRUSTrust-1.93.0Success85,691
Jan 30, 2026 17:10olliecrowolliecrowRUSTrust-1.93.0Success1,130,429
Jan 30, 2026 17:09olliecrowolliecrowRUSTrust-1.93.0Success90,009
Jan 30, 2026 17:08olliecrowolliecrowCPPg++13.3.0Success9,881
Jan 30, 2026 17:07olliecrowolliecrowCPPg++13.3.0Error
Jan 30, 2026 17:06olliecrowolliecrowCPPg++13.3.0Success9,231+66.84 RP
Jan 30, 2026 17:06olliecrowolliecrowCPPg++13.3.0Error
Jan 30, 2026 17:04olliecrowolliecrowCPPg++13.3.0Success9,838+58.43 RP
Jan 30, 2026 17:03olliecrowolliecrowCPPg++13.3.0Success10,438+107.84 RP
Jan 30, 2026 17:02olliecrowolliecrowCPPg++13.3.0Success11,762+156.67 RP
Jan 30, 2026 17:01olliecrowolliecrowCPPg++13.3.0Error
Jan 30, 2026 16:59olliecrowolliecrowCPPg++13.3.0Success14,419+544.69 RP
Jan 30, 2026 16:39olliecrowolliecrowCPPg++13.3.0Success114,229
Jan 30, 2026 16:39olliecrowolliecrowCPPg++13.3.0Success115,367
Jan 30, 2026 16:36olliecrowolliecrowRUSTrust-1.93.0Success271,728
Jan 30, 2026 16:36olliecrowolliecrowRUSTrust-1.93.0Success67,186+40.55 RP
Jan 30, 2026 15:58olliecrowolliecrowCPPg++13.3.0Success99,793
Jan 30, 2026 15:41olliecrowolliecrowCPPg++13.3.0Success92,341+5.55 RP
Jan 30, 2026 15:40olliecrowolliecrowCPPg++13.3.0Success107,578
Jan 30, 2026 15:32olliecrowolliecrowRUSTrust-1.93.0Success134,445
Jan 30, 2026 14:27olliecrowolliecrowRUSTrust-1.93.0Success97,326+10.02 RP
Jan 30, 2026 14:26olliecrowolliecrowRUSTrust-1.93.0Success113,705
Jan 30, 2026 14:25olliecrowolliecrowCPPg++13.3.0Success107,845+12.46 RP
Jan 30, 2026 14:25olliecrowolliecrowCPPg++13.3.0Success124,593+80.26 RP
Jan 27, 2026 08:17RussellStrongRussellStrongRUSTrust-1.93.0Success106,698
Jan 27, 2026 08:06RussellStrongRussellStrongRUSTrust-1.93.0Success105,747
Jan 27, 2026 07:40RussellStrongRussellStrongRUSTrust-1.93.0Success115,271
Jan 21, 2026 06:49p-rup-ruCPPclang++18.1.3Success448,595
Jan 21, 2026 05:12AlionAlionRUSTrust-1.92.0Success61,160+12.79 RP
Jan 21, 2026 05:07AlionAlionRUSTrust-1.92.0Success68,041
Jan 21, 2026 05:06AlionAlionRUSTrust-1.92.0Error
Jan 21, 2026 04:19AlionAlionRUSTrust-1.92.0Success67,162
Jan 21, 2026 04:18AlionAlionRUSTrust-1.92.0Success66,352+7.69 RP
Jan 21, 2026 04:18AlionAlionRUSTrust-1.92.0Success69,921+22.03 RP
Jan 21, 2026 04:14AlionAlionRUSTrust-1.92.0Success82,653+14.49 RP
Jan 21, 2026 04:12AlionAlionRUSTrust-1.92.0Success96,597
Jan 21, 2026 04:10AlionAlionRUSTrust-1.92.0Success106,678
Jan 21, 2026 04:10AlionAlionRUSTrust-1.92.0Success93,903+16.29 RP
Jan 21, 2026 04:09AlionAlionRUSTrust-1.92.0Success110,867+28.40 RP
Jan 21, 2026 04:08AlionAlionRUSTrust-1.92.0Success161,810+31.06 RP
Jan 21, 2026 04:06AlionAlionRUSTrust-1.92.0Success325,319+30.74 RP
Jan 21, 2026 00:37Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success93,217
Jan 21, 2026 00:36Liquidity GoblinLiquidity GoblinRUSTrust-1.92.0Success96,393
Jan 19, 2026 18:22Josu San MartinJosu San MartinCPPg++13.3.0Error
Jan 17, 2026 04:52RussellStrongRussellStrongRUSTrust-1.92.0Success108,664
Jan 17, 2026 04:24RussellStrongRussellStrongRUSTrust-1.92.0Success119,157
Jan 17, 2026 04:24RussellStrongRussellStrongRUSTrust-1.92.0Success119,126
Jan 14, 2026 05:34RussellStrongRussellStrongRUSTrust-1.92.0Success231,453
Jan 14, 2026 05:29RussellStrongRussellStrongRUSTrust-1.92.0Success218,866
Jan 14, 2026 02:04RussellStrongRussellStrongRUSTrust-1.92.0Success248,667
Jan 14, 2026 02:03RussellStrongRussellStrongRUSTrust-1.92.0Success253,084
Jan 14, 2026 01:15RussellStrongRussellStrongRUSTrust-1.92.0Success104,955
Jan 14, 2026 01:14RussellStrongRussellStrongRUSTrust-1.92.0Success106,888
Jan 14, 2026 01:14RussellStrongRussellStrongRUSTrust-1.92.0Success104,255
Jan 14, 2026 01:13RussellStrongRussellStrongRUSTrust-1.92.0Success104,172
Jan 14, 2026 01:12RussellStrongRussellStrongRUSTrust-1.92.0Success248,326
Jan 14, 2026 01:11RussellStrongRussellStrongRUSTrust-1.92.0Success265,028
Jan 14, 2026 01:09RussellStrongRussellStrongRUSTrust-1.92.0Success251,207
Jan 14, 2026 00:47RussellStrongRussellStrongRUSTrust-1.92.0Success255,205
Jan 11, 2026 11:14Tim ZhamoidinTim ZhamoidinCPPclang++18.1.3Success94,905
Jan 10, 2026 17:04Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Error
Jan 10, 2026 17:02Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Error
Jan 9, 2026 23:18Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Error
Jan 9, 2026 23:13Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,798
Jan 9, 2026 22:21Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,840
Jan 9, 2026 22:10Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,140
Jan 9, 2026 21:55Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,240
Jan 9, 2026 21:30Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success108,110
Jan 9, 2026 21:26Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success106,724+0.77 RP
Jan 9, 2026 21:21Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,912
Jan 9, 2026 21:19Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Error
Jan 9, 2026 21:17Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success108,519
Jan 9, 2026 20:59Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success108,797
Jan 9, 2026 20:55Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success109,069
Jan 9, 2026 20:50Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success107,607+13.85 RP
Jan 9, 2026 20:27Juraj ZelmanJuraj ZelmanRUSTrust-1.91.1Success126,447+79.08 RP
Jan 8, 2026 16:44AriiellusAriiellusRUSTrust-1.92.0Success336,009+3.81 RP
Jan 8, 2026 16:23AriiellusAriiellusRUSTrust-1.92.0Success385,322+0.20 RP
Jan 8, 2026 06:00AriiellusAriiellusRUSTrust-1.92.0Success388,264+25.76 RP