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
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
Apr 27, 2026 12:48Victor MerckléVictor MerckléCPPg++14.2.0Success380,755
Apr 27, 2026 12:28Victor MerckléVictor MerckléCPPg++14.2.0Success107,634+12.45 RP
Apr 27, 2026 02:57olliecrowolliecrowCSHARP9.0.14Success61,027
Apr 27, 2026 02:13olliecrowolliecrowGOgo1.26.2Success118,648
Apr 27, 2026 01:49olliecrowolliecrowRUSTrust-1.95.0Success55,001
Apr 26, 2026 23:29Victor MerckléVictor MerckléCPPg++14.2.0Success124,293+27.80 RP
Apr 26, 2026 23:26Victor MerckléVictor MerckléCPPg++14.2.0Success366,660
Apr 26, 2026 23:19Victor MerckléVictor MerckléCPPg++14.2.0Success251,691
Apr 26, 2026 23:16Victor MerckléVictor MerckléCPPg++14.2.0Success189,912+26.64 RP
Apr 26, 2026 23:08Victor MerckléVictor MerckléCPPg++14.2.0Success644,660
Apr 26, 2026 23:00Victor MerckléVictor MerckléCPPg++14.2.0Success683,222
Apr 26, 2026 22:57Victor MerckléVictor MerckléCPPg++14.2.0Success384,420+0.08 RP
Apr 26, 2026 22:37Victor MerckléVictor MerckléCPPg++14.2.0Success385,582+3.24 RP
Apr 26, 2026 22:27Victor MerckléVictor MerckléCPPg++14.2.0Success440,656+22.69 RP
Apr 26, 2026 20:45TudyMTudyMCPPg++14.2.0Success233,889+0.82 RP
Apr 26, 2026 19:56TudyMTudyMCPPg++14.2.0Success238,437+0.65 RP
Apr 26, 2026 19:55TudyMTudyMCPPclang++20.1.2Success248,410
Apr 26, 2026 18:39TudyMTudyMCPPg++10.5.0Success242,194+10.95 RP
Apr 26, 2026 18:38TudyMTudyMCPPg++10.5.0Success329,601+9.50 RP
Apr 26, 2026 18:36TudyMTudyMCPPg++10.5.0Success479,948+13.20 RP
Apr 26, 2026 17:31TudyMTudyMCPPg++10.5.0Success1,309,422+0.14 RP
Apr 26, 2026 17:26TudyMTudyMCPPg++10.5.0Success1,334,587+7.49 RP
Apr 26, 2026 08:54olliecrowolliecrowZIG0.15.1Success53,213
Apr 26, 2026 08:33olliecrowolliecrowCSHARP9.0.15Success60,401
Apr 26, 2026 02:23olliecrowolliecrowZIG0.15.1Success53,315
Apr 26, 2026 02:18olliecrowolliecrowZIG0.15.1Error
Apr 26, 2026 02:01olliecrowolliecrowCSHARP9.0.15Success113,012
Apr 25, 2026 21:10olliecrowolliecrowZIG0.15.1Success63,336
Apr 25, 2026 20:28Charles CabergsCharles CabergsCPPg++13.3.0Success44,353
Apr 25, 2026 20:02olliecrowolliecrowCSHARP9.0.15Success104,829
Apr 25, 2026 16:23Weitao WanWeitao WanCPPg++10.5.0Success267,924
Apr 25, 2026 13:14olliecrowolliecrowZIG0.15.1Success51,594
Apr 25, 2026 13:01olliecrowolliecrowCSHARP9.0.15Success74,834
Apr 25, 2026 12:39olliecrowolliecrowZIG0.15.1Success54,643
Apr 25, 2026 12:25olliecrowolliecrowCSHARP9.0.15Success67,251
Apr 25, 2026 11:22olliecrowolliecrowZIG0.15.1Success65,496
Apr 25, 2026 11:03olliecrowolliecrowZIG0.15.1Success51,355
Apr 25, 2026 10:37olliecrowolliecrowCSHARP9.0.15Success101,287
Apr 25, 2026 03:15olliecrowolliecrowZIG0.15.1Success66,667
Apr 25, 2026 02:58olliecrowolliecrowCSHARP9.0.15Success67,410
Apr 24, 2026 11:00Charles CabergsCharles CabergsCPPg++13.3.0Success41,310+12.21 RP