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 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
Apr 19, 2026 08:01olliecrowolliecrowCPPclang++18.1.3Success98,064
Apr 19, 2026 07:53olliecrowolliecrowCPPclang++18.1.3Success7,369
Apr 19, 2026 07:52olliecrowolliecrowCPPclang++18.1.3Error
Apr 19, 2026 05:41Charles CabergsCharles CabergsCPPg++13.3.0Success54,998
Apr 19, 2026 04:07olliecrowolliecrowCPPclang++18.1.3Success99,024
Apr 19, 2026 04:05olliecrowolliecrowCPPclang++18.1.3Success93,281
Apr 19, 2026 04:03olliecrowolliecrowCPPclang++18.1.3Success100,566
Apr 19, 2026 03:36Charles CabergsCharles CabergsCPPg++13.3.0Success54,547
Apr 19, 2026 01:52olliecrowolliecrowCSHARP9.0.14Error
Apr 19, 2026 01:21Charles CabergsCharles CabergsCPPg++13.3.0Success56,379
Apr 18, 2026 22:31olliecrowolliecrowZIG0.14.0Success439,809
Apr 18, 2026 22:20olliecrowolliecrowRUSTrust-1.94.0Success88,886
Apr 18, 2026 22:09olliecrowolliecrowGOgo1.26.1Success123,110
Apr 18, 2026 21:50olliecrowolliecrowCSHARP9.0.14Success114,067
Apr 18, 2026 21:48olliecrowolliecrowCSHARP9.0.14Error
Apr 18, 2026 20:46Charles CabergsCharles CabergsCPPg++13.3.0Success53,526+76.45 RP
Apr 18, 2026 14:18Charles CabergsCharles CabergsCPPg++13.3.0Success90,598+26.26 RP
Apr 18, 2026 04:46Charles CabergsCharles CabergsCPPg++13.3.0Success118,886+12.65 RP
Apr 18, 2026 01:34olliecrowolliecrowGOgo1.26.1Error
Apr 18, 2026 01:32olliecrowolliecrowGOgo1.26.1Error
Apr 18, 2026 01:28olliecrowolliecrowGOgo1.26.1Success9,391
Apr 18, 2026 01:18olliecrowolliecrowCSHARP9.0.14Error
Apr 18, 2026 01:16olliecrowolliecrowCSHARP9.0.14Error
Apr 18, 2026 01:14olliecrowolliecrowCSHARP9.0.14Error