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
May 4, 2026 09:40Victor MerckléVictor MerckléCPPg++14.2.0Success274,008
May 4, 2026 09:23Victor MerckléVictor MerckléCPPg++14.2.0Success682,444
May 1, 2026 15:55Victor MerckléVictor MerckléCPPg++14.2.0Success436,515
May 1, 2026 15:48Victor MerckléVictor MerckléCPPg++14.2.0Success437,674
May 1, 2026 15:35Victor MerckléVictor MerckléCPPg++14.2.0Success423,648
May 1, 2026 15:22Victor MerckléVictor MerckléCPPg++14.2.0Success421,546
May 1, 2026 14:35Victor MerckléVictor MerckléCPPg++14.2.0Success391,505
May 1, 2026 00:15olliecrowolliecrowZIG0.15.1Success50,624
Apr 30, 2026 23:58olliecrowolliecrowCSHARP9.0.15Success112,629
Apr 30, 2026 23:47olliecrowolliecrowGOgo1.26.2Success127,220
Apr 30, 2026 23:36olliecrowolliecrowRUSTrust-1.94.0Success54,520
Apr 30, 2026 23:18olliecrowolliecrowCPPclang++18.1.3Success44,951
Apr 30, 2026 13:24olliecrowolliecrowCSHARP9.0.15Success112,287
Apr 30, 2026 12:54olliecrowolliecrowGOgo1.26.2Success123,984
Apr 30, 2026 12:42olliecrowolliecrowRUSTrust-1.95.0Success55,903
Apr 30, 2026 12:29olliecrowolliecrowCPPg++10.5.0Success57,258
Apr 30, 2026 11:53olliecrowolliecrowZIG0.15.1Success52,191
Apr 30, 2026 11:39olliecrowolliecrowCSHARP9.0.15Success111,010
Apr 30, 2026 11:18olliecrowolliecrowRUSTrust-1.94.0Success86,144
Apr 30, 2026 11:06olliecrowolliecrowCPPg++10.5.0Success52,512
Apr 30, 2026 11:03olliecrowolliecrowCPPg++10.5.0Success55,196
Apr 29, 2026 18:22olliecrowolliecrowZIG0.15.1Success52,524
Apr 29, 2026 18:15olliecrowolliecrowZIG0.15.1Success52,706
Apr 29, 2026 17:31olliecrowolliecrowCSHARP9.0.15Success90,653
Apr 29, 2026 15:23Danylo MocherniukDanylo MocherniukCPPg++10.5.0Error
Apr 29, 2026 15:15Danylo MocherniukDanylo MocherniukCPPg++14.2.0Error
Apr 29, 2026 15:14Danylo MocherniukDanylo MocherniukCPPg++14.2.0Error
Apr 29, 2026 14:58Danylo MocherniukDanylo MocherniukCPPg++10.5.0Success1,303,794+7.67 RP
Apr 29, 2026 10:54limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 10:52limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 10:46limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 10:32limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 10:29limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 10:26limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 09:00Charles CabergsCharles CabergsCPPg++13.3.0Success44,141
Apr 29, 2026 08:33limanjun99limanjun99CPPg++14.2.0Success45,201+9.51 RP
Apr 29, 2026 08:31limanjun99limanjun99CPPg++14.2.0Error
Apr 29, 2026 08:04olliecrowolliecrowZIG0.15.1Success48,236
Apr 29, 2026 07:48olliecrowolliecrowCSHARP9.0.15Success110,325
Apr 29, 2026 02:22olliecrowolliecrowZIG0.15.1Success57,562
Apr 29, 2026 02:01olliecrowolliecrowCSHARP9.0.15Success110,365
Apr 29, 2026 01:00Charles CabergsCharles CabergsCPPg++13.3.0Success44,677
Apr 29, 2026 00:40olliecrowolliecrowZIG0.15.1Success49,944
Apr 28, 2026 17:59olliecrowolliecrowZIG0.15.1Success57,244
Apr 28, 2026 17:43olliecrowolliecrowCSHARP9.0.15Success63,489
Apr 28, 2026 17:18olliecrowolliecrowGOgo1.26.2Success85,962
Apr 28, 2026 17:02olliecrowolliecrowRUSTrust-1.95.0Success62,510
Apr 28, 2026 16:49olliecrowolliecrowCPPg++10.5.0Success86,831
Apr 28, 2026 16:20limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 16:03limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 15:00limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 14:56limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 14:53limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 14:53Charles CabergsCharles CabergsCPPg++13.3.0Success41,744
Apr 28, 2026 13:58Dominique GarmierDominique GarmierRUSTrust-1.94.1Error
Apr 28, 2026 13:33limanjun99limanjun99CPPg++14.2.0Error
Apr 28, 2026 13:19Dominique GarmierDominique GarmierCPPg++13.3.0Error
Apr 28, 2026 12:39Dominique GarmierDominique GarmierCPPg++13.3.0Success83,372
Apr 28, 2026 10:18Victor MerckléVictor MerckléCPPg++14.2.0Success67,496
Apr 28, 2026 10:16TudyMTudyMCPPg++10.5.0Success370,720
Apr 28, 2026 10:07TudyMTudyMCPPg++14.2.0Success136,539
Apr 28, 2026 10:06TudyMTudyMCPPg++10.5.0Success358,065
Apr 28, 2026 10:04TudyMTudyMCPPg++14.2.0Success268,082
Apr 28, 2026 09:57TudyMTudyMCPPg++14.2.0Success903,582
Apr 28, 2026 06:16Victor MerckléVictor MerckléCPPg++14.2.0Success33,105+0.16 RP
Apr 28, 2026 05:23Victor MerckléVictor MerckléCPPg++14.2.0Success387,486
Apr 27, 2026 21:29TudyMTudyMCPPg++14.2.0Success213,405
Apr 27, 2026 21:21TudyMTudyMCPPg++14.2.0Error
Apr 27, 2026 21:21TudyMTudyMCPPg++14.2.0Error
Apr 27, 2026 21:20TudyMTudyMCPPg++14.2.0Error
Apr 27, 2026 21:14TudyMTudyMCPPg++14.2.0Success134,134+0.74 RP
Apr 27, 2026 21:13TudyMTudyMCPPg++14.2.0Success149,182
Apr 27, 2026 21:11TudyMTudyMCPPg++14.2.0Success135,487+2.77 RP
Apr 27, 2026 21:07TudyMTudyMCPPg++14.2.0Success149,981
Apr 27, 2026 21:05TudyMTudyMCPPg++14.2.0Success140,779+3.25 RP
Apr 27, 2026 21:02TudyMTudyMCPPg++14.2.0Success147,536+4.91 RP
Apr 27, 2026 21:00TudyMTudyMCPPg++14.2.0Success159,048+2.55 RP
Apr 27, 2026 20:57TudyMTudyMCPPg++14.2.0Success165,772+3.60 RP
Apr 27, 2026 20:53TudyMTudyMCPPg++14.2.0Success183,181
Apr 27, 2026 20:52TudyMTudyMCPPg++14.2.0Success186,668
Apr 27, 2026 20:50TudyMTudyMCPPg++14.2.0Success176,293+2.04 RP
Apr 27, 2026 20:49TudyMTudyMCPPg++14.2.0Success182,877+1.72 RP
Apr 27, 2026 20:48TudyMTudyMCPPg++14.2.0Success188,820+0.01 RP
Apr 27, 2026 20:46TudyMTudyMCPPg++14.2.0Success188,870+1.32 RP
Apr 27, 2026 20:43TudyMTudyMCPPg++14.2.0Success196,784
Apr 27, 2026 20:40TudyMTudyMCPPg++14.2.0Success202,163
Apr 27, 2026 20:36TudyMTudyMCPPg++14.2.0Success203,808
Apr 27, 2026 20:34TudyMTudyMCPPg++14.2.0Success193,710+0.46 RP
Apr 27, 2026 20:32TudyMTudyMCPPg++14.2.0Success195,453+1.75 RP
Apr 27, 2026 20:11Victor MerckléVictor MerckléCPPg++14.2.0Success33,122
Apr 27, 2026 20:06Victor MerckléVictor MerckléCPPg++14.2.0Success33,122+45.46 RP
Apr 27, 2026 19:58TudyMTudyMCPPg++14.2.0Success202,362+1.76 RP
Apr 27, 2026 19:56TudyMTudyMCPPg++14.2.0Success209,837+0.99 RP
Apr 27, 2026 19:55TudyMTudyMCPPg++14.2.0Success214,287+3.58 RP
Apr 27, 2026 19:51TudyMTudyMCPPg++14.2.0Success232,089+0.33 RP
Apr 27, 2026 15:43Victor MerckléVictor MerckléCPPg++14.2.0Success38,993+51.43 RP
Apr 27, 2026 13:52Victor MerckléVictor MerckléCPPg++14.2.0Success50,305
Apr 27, 2026 13:51Victor MerckléVictor MerckléCPPg++14.2.0Success50,682
Apr 27, 2026 13:49Victor MerckléVictor MerckléCPPg++14.2.0Success48,775+5.51 RP
Apr 27, 2026 13:27Victor MerckléVictor MerckléCPPg++14.2.0Success50,122+106.61 RP