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 20, 2026 20:09M-RochatM-RochatCPPg++13.3.0Success7,929+65.74 RP
May 20, 2026 20:05M-RochatM-RochatCPPg++13.3.0Success8,365+86.56 RP
May 20, 2026 20:02M-RochatM-RochatCPPg++13.3.0Error
May 20, 2026 20:00M-RochatM-RochatCPPg++13.3.0Success9,018+51.36 RP
May 20, 2026 19:57M-RochatM-RochatCPPg++13.3.0Success9,456+107.41 RP
May 20, 2026 19:55M-RochatM-RochatCPPg++13.3.0Success10,525+6.37 RP
May 20, 2026 19:53M-RochatM-RochatCPPg++13.3.0Error
May 20, 2026 19:48M-RochatM-RochatCPPg++13.3.0Success10,667
May 20, 2026 19:47M-RochatM-RochatCPPg++13.3.0Success10,596+7.77 RP
May 20, 2026 19:44M-RochatM-RochatCPPg++13.3.0Success10,787
May 20, 2026 19:36M-RochatM-RochatCPPg++13.3.0Success10,684+15.00 RP
May 20, 2026 19:32M-RochatM-RochatCPPg++13.3.0Error
May 20, 2026 19:31M-RochatM-RochatCPPg++13.3.0Error
May 19, 2026 14:43M-RochatM-RochatCPPg++13.3.0Success421,686
May 18, 2026 17:04M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 10:27M-RochatM-RochatCPPg++13.3.0Success11,287
May 17, 2026 10:27M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 10:27M-RochatM-RochatCPPg++13.3.0Success11,068
May 17, 2026 09:44M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:44M-RochatM-RochatCPPg++13.3.0Success11,005
May 17, 2026 09:44M-RochatM-RochatCPPg++13.3.0Success11,174
May 17, 2026 09:44M-RochatM-RochatCPPg++13.3.0Success10,953
May 17, 2026 09:43M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:43M-RochatM-RochatCPPg++13.3.0Success10,889
May 17, 2026 09:42M-RochatM-RochatCPPg++13.3.0Success10,858+5.82 RP
May 17, 2026 09:42M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:42M-RochatM-RochatCPPg++13.3.0Success11,181
May 17, 2026 09:41M-RochatM-RochatCPPg++14.2.0Error
May 17, 2026 09:41M-RochatM-RochatCPPg++13.3.0Success10,927+6.16 RP
May 17, 2026 09:41M-RochatM-RochatCPPg++10.5.0Error
May 17, 2026 09:39M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:39M-RochatM-RochatCPPg++13.3.0Success11,234
May 17, 2026 09:39M-RochatM-RochatCPPg++13.3.0Success11,191
May 17, 2026 09:38M-RochatM-RochatCPPg++13.3.0Success11,081
May 17, 2026 09:38M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:38M-RochatM-RochatCPPg++13.3.0Success11,360
May 17, 2026 09:37M-RochatM-RochatCPPg++13.3.0Success11,022
May 17, 2026 09:37M-RochatM-RochatCPPg++13.3.0Success11,208
May 17, 2026 09:37M-RochatM-RochatCPPg++13.3.0Success11,001+6.97 RP
May 17, 2026 09:36M-RochatM-RochatCPPg++13.3.0Success11,086+4.45 RP
May 17, 2026 09:36M-RochatM-RochatCPPg++13.3.0Success11,141+0.40 RP
May 17, 2026 09:36M-RochatM-RochatCPPg++13.3.0Success11,146+6.87 RP
May 17, 2026 09:35M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 09:35M-RochatM-RochatCPPg++13.3.0Success11,232+2.84 RP
May 17, 2026 09:34M-RochatM-RochatCPPg++13.3.0Error
May 17, 2026 08:15M-RochatM-RochatCPPg++13.3.0Success11,593
May 17, 2026 02:56M-RochatM-RochatCPPg++13.3.0Success11,581
May 17, 2026 02:56M-RochatM-RochatCPPg++13.3.0Success11,505
May 17, 2026 02:55M-RochatM-RochatCPPg++13.3.0Success11,472
May 17, 2026 02:55M-RochatM-RochatCPPg++13.3.0Success11,532
May 17, 2026 02:53M-RochatM-RochatCPPg++13.3.0Success11,268+9.27 RP
May 17, 2026 00:44M-RochatM-RochatCPPg++13.3.0Success11,568
May 17, 2026 00:44M-RochatM-RochatCPPg++13.3.0Success11,544
May 17, 2026 00:44M-RochatM-RochatCPPg++13.3.0Success11,570
May 16, 2026 22:18M-RochatM-RochatCPPg++13.3.0Success11,565
May 16, 2026 22:18M-RochatM-RochatCPPg++13.3.0Success11,675
May 16, 2026 17:23M-RochatM-RochatCPPg++14.2.0Error
May 16, 2026 17:22M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:21M-RochatM-RochatCPPg++13.3.0Success11,387+11.04 RP
May 16, 2026 17:21M-RochatM-RochatCPPg++13.3.0Success11,532+171.26 RP
May 16, 2026 17:19M-RochatM-RochatCPPg++13.3.0Success14,525
May 16, 2026 17:18M-RochatM-RochatCPPg++13.3.0Success14,370+24.89 RP
May 16, 2026 17:16M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:15M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:15M-RochatM-RochatCPPg++13.3.0Success14,903+34.27 RP
May 16, 2026 17:14M-RochatM-RochatCPPg++13.3.0Success17,008
May 16, 2026 17:14M-RochatM-RochatCPPg++13.3.0Success15,705+83.03 RP
May 16, 2026 17:13M-RochatM-RochatCPPg++13.3.0Success22,048
May 16, 2026 17:13M-RochatM-RochatCPPg++13.3.0Success18,060+139.24 RP
May 16, 2026 17:12M-RochatM-RochatCPPg++13.3.0Success37,365
May 16, 2026 17:11M-RochatM-RochatCPPg++13.3.0Success24,127+217.11 RP
May 16, 2026 17:11M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:11M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:11M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 17:02M-RochatM-RochatCPPg++13.3.0Success50,667+159.86 RP
May 16, 2026 16:07M-RochatM-RochatCPPg++13.3.0Error
May 16, 2026 08:59M-RochatM-RochatCPPg++13.3.0Success3,666,525
May 16, 2026 08:58M-RochatM-RochatCPPg++13.3.0Success266,593+0.16 RP
May 16, 2026 08:57M-RochatM-RochatCPPg++13.3.0Success273,436
May 16, 2026 08:55M-RochatM-RochatCPPg++13.3.0Success378,900
May 15, 2026 22:09Josu San MartinJosu San MartinCPPg++10.5.0Error
May 15, 2026 22:06Josu San MartinJosu San MartinCPPg++10.5.0Error
May 15, 2026 19:38Josu San MartinJosu San MartinCPPg++10.5.0Error
May 15, 2026 19:37M-RochatM-RochatCPPg++13.3.0Success267,758+37.35 RP
May 14, 2026 17:15Josu San MartinJosu San MartinCSHARP9.0.13Success1,346,456
May 14, 2026 14:44Josu San MartinJosu San MartinGOgo1.25.5Error
May 14, 2026 14:14CadovvlCadovvlCPPg++13.3.0Error
May 14, 2026 14:13CadovvlCadovvlCPPg++13.3.0Error
May 11, 2026 14:48Chang Jia JunChang Jia JunCPPg++10.5.0Error
May 8, 2026 17:25Hung T.NguyenHung T.NguyenCPPg++10.5.0Success327,637
May 8, 2026 16:29Hung T.NguyenHung T.NguyenCPPg++10.5.0Success253,181+39.50 RP
May 8, 2026 08:56Ashish AhujaAshish AhujaCPPg++10.5.0Success661,758+0.10 RP
May 8, 2026 08:56Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:55Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:47Ashish AhujaAshish AhujaCPPg++10.5.0Success665,977+7.30 RP
May 8, 2026 08:44Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:39Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:31Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:30Ashish AhujaAshish AhujaCPPg++10.5.0Error
May 8, 2026 08:24Ashish AhujaAshish AhujaCPPg++10.5.0Error