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 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
Apr 24, 2026 09:03olliecrowolliecrowZIG0.15.1Success52,313
Apr 24, 2026 08:36olliecrowolliecrowCSHARP9.0.15Success69,803
Apr 24, 2026 08:35Dominique GarmierDominique GarmierRUSTrust-1.94.1Error
Apr 24, 2026 04:11Charles CabergsCharles CabergsCPPg++13.3.0Success43,505+4.14 RP
Apr 23, 2026 21:59olliecrowolliecrowZIG0.15.1Success97,463
Apr 23, 2026 21:42olliecrowolliecrowCSHARP9.0.15Success109,698
Apr 23, 2026 20:13olliecrowolliecrowZIG0.15.1Success62,008
Apr 23, 2026 20:00olliecrowolliecrowCSHARP9.0.15Success67,044
Apr 23, 2026 11:46Dominique GarmierDominique GarmierCPPg++13.3.0Success66,898
Apr 23, 2026 10:22limanjun99limanjun99CPPg++14.2.0Success47,672
Apr 23, 2026 10:14limanjun99limanjun99CPPg++14.2.0Success47,979
Apr 23, 2026 09:48limanjun99limanjun99CPPg++14.2.0Success47,779
Apr 23, 2026 09:40limanjun99limanjun99CPPg++14.2.0Success48,251
Apr 23, 2026 09:32limanjun99limanjun99CPPg++14.2.0Success47,243
Apr 23, 2026 09:26limanjun99limanjun99CPPg++14.2.0Success48,348
Apr 23, 2026 09:23limanjun99limanjun99CPPg++14.2.0Success55,179
Apr 23, 2026 09:03limanjun99limanjun99CPPg++14.2.0Success47,232+8.33 RP
Apr 23, 2026 08:55limanjun99limanjun99CPPg++14.2.0Success49,167+3.92 RP
Apr 23, 2026 08:43limanjun99limanjun99CPPg++14.2.0Error
Apr 23, 2026 08:42limanjun99limanjun99CPPg++14.2.0Success50,134+4.37 RP
Apr 23, 2026 08:31limanjun99limanjun99CPPg++14.2.0Success51,796
Apr 23, 2026 08:28limanjun99limanjun99CPPg++14.2.0Success51,256+14.33 RP
Apr 23, 2026 08:11limanjun99limanjun99CPPg++14.2.0Success55,432
Apr 23, 2026 06:54Charles CabergsCharles CabergsCPPg++13.3.0Success44,303+3.28 RP
Apr 23, 2026 05:22limanjun99limanjun99CPPg++14.2.0Error
Apr 23, 2026 05:20limanjun99limanjun99CPPg++14.2.0Success61,675
Apr 23, 2026 05:17limanjun99limanjun99CPPg++14.2.0Success55,318+32.86 RP
Apr 23, 2026 04:45Charles CabergsCharles CabergsCPPg++13.3.0Error
Apr 23, 2026 02:46Charles CabergsCharles CabergsCPPg++13.3.0Success44,956+1.06 RP
Apr 22, 2026 22:34olliecrowolliecrowCSHARP9.0.15Success90,991
Apr 22, 2026 20:51Dominique GarmierDominique GarmierCPPg++10.5.0Success8,975
Apr 22, 2026 16:25olliecrowolliecrowZIG0.15.1Success86,068
Apr 22, 2026 16:07olliecrowolliecrowCSHARP9.0.15Success109,765
Apr 22, 2026 15:51olliecrowolliecrowGOgo1.26.2Success121,965
Apr 22, 2026 15:36olliecrowolliecrowRUSTrust-1.95.0Success86,748
Apr 22, 2026 15:23olliecrowolliecrowCPPg++10.5.0Success81,079
Apr 22, 2026 13:17BenBenCPPg++14.2.0Success136,350
Apr 22, 2026 07:46limanjun99limanjun99CPPg++14.2.0Success67,606+20.42 RP
Apr 22, 2026 07:39olliecrowolliecrowZIG0.15.1Success85,168
Apr 22, 2026 07:37limanjun99limanjun99CPPg++14.2.0Error
Apr 22, 2026 07:28olliecrowolliecrowRUSTrust-1.95.0Success86,206
Apr 22, 2026 07:18limanjun99limanjun99CPPg++14.2.0Success78,432+11.28 RP
Apr 22, 2026 07:10olliecrowolliecrowGOgo1.26.2Success124,006
Apr 22, 2026 06:54olliecrowolliecrowCSHARP9.0.15Success110,696
Apr 22, 2026 06:29olliecrowolliecrowCPPg++10.5.0Success78,029
Apr 22, 2026 05:18limanjun99limanjun99CPPg++14.2.0Success90,368
Apr 22, 2026 05:13limanjun99limanjun99CPPg++14.2.0Success86,515
Apr 22, 2026 05:10limanjun99limanjun99CPPg++14.2.0Success86,046+4.91 RP
Apr 22, 2026 05:06limanjun99limanjun99CPPg++14.2.0Success107,441
Apr 22, 2026 05:04limanjun99limanjun99CPPg++14.2.0Error
Apr 22, 2026 04:57limanjun99limanjun99CPPg++14.2.0Success89,844+40.54 RP
Apr 22, 2026 04:37limanjun99limanjun99CPPg++14.2.0Success141,313+64.19 RP
Apr 22, 2026 04:24limanjun99limanjun99CPPg++14.2.0Error
Apr 21, 2026 22:00olliecrowolliecrowCPPg++10.5.0Success96,439
Apr 21, 2026 21:53Charles CabergsCharles CabergsCPPg++13.3.0Success45,217
Apr 21, 2026 18:58olliecrowolliecrowCPPg++10.5.0Success75,484
Apr 21, 2026 18:55Charles CabergsCharles CabergsCPPg++13.3.0Success45,172+11.42 RP
Apr 21, 2026 16:53Charles CabergsCharles CabergsCPPg++13.3.0Success47,629+23.13 RP
Apr 21, 2026 15:50olliecrowolliecrowCPPg++10.5.0Success96,681
Apr 21, 2026 15:37limanjun99limanjun99CPPg++14.2.0Success1,520,232+6.58 RP
Apr 21, 2026 10:18Charles CabergsCharles CabergsCPPg++13.3.0Success55,181
Apr 21, 2026 09:38Dominique GarmierDominique GarmierCPPg++13.3.0Success83,134
Apr 21, 2026 09:32Dominique GarmierDominique GarmierCPPg++13.3.0Success11,606
Apr 21, 2026 08:29Charles CabergsCharles CabergsCPPg++13.3.0Error
Apr 21, 2026 07:09Dominique GarmierDominique GarmierCPPg++13.3.0Success144,250
Apr 21, 2026 02:07olliecrowolliecrowCPPg++10.5.0Success76,506
Apr 21, 2026 01:30olliecrowolliecrowCPPg++10.5.0Success77,820
Apr 21, 2026 01:28Taylor DettlingTaylor DettlingCPPg++10.5.0Success1,297,434+7.71 RP
Apr 21, 2026 00:55olliecrowolliecrowCPPg++13.3.0Success94,532
Apr 20, 2026 21:02Charles CabergsCharles CabergsCPPg++13.3.0Success85,512
Apr 20, 2026 18:23olliecrowolliecrowCPPg++13.3.0Success78,520
Apr 20, 2026 18:07Charles CabergsCharles CabergsCPPg++13.3.0Success53,558
Apr 20, 2026 16:53olliecrowolliecrowCPPg++10.5.0Success75,215
Apr 20, 2026 16:33BenBenCPPg++14.2.0Success111,943+2.59 RP
Apr 20, 2026 16:33BenBenCPPclang++20.1.2Success115,286+13.48 RP
Apr 20, 2026 16:30BenBenCPPg++14.2.0Success136,493+0.83 RP
Apr 20, 2026 16:30BenBenCPPclang++18.1.3Success139,563
Apr 20, 2026 16:30BenBenCPPclang++20.1.2Success138,058+0.78 RP
Apr 20, 2026 16:30BenBenCPPclang++20.1.2Success139,556+71.66 RP
Apr 20, 2026 15:00olliecrowolliecrowZIG0.14.0Success87,391
Apr 20, 2026 14:59olliecrowolliecrowRUSTrust-1.94.0Error
Apr 20, 2026 14:58olliecrowolliecrowGOgo1.26.1Success124,908
Apr 20, 2026 14:54olliecrowolliecrowCPPg++13.3.0Success6,268