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 5, 2026 20:28olliecrowolliecrowCSHARP9.0.15Success125,755
May 5, 2026 20:12olliecrowolliecrowCSHARP9.0.15Success115,456
May 5, 2026 16:37olliecrowolliecrowCSHARP9.0.15Success77,939
May 5, 2026 16:16olliecrowolliecrowCSHARP9.0.14Success92,241
May 5, 2026 15:55Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 15:52Victor MerckléVictor MerckléCPPg++14.2.0Success36,794
May 5, 2026 15:36olliecrowolliecrowCSHARP9.0.14Success101,203
May 5, 2026 14:58Victor MerckléVictor MerckléGOgo1.26.2Success8,463
May 5, 2026 14:49Victor MerckléVictor MerckléGOgo1.26.2Error
May 5, 2026 14:38Victor MerckléVictor MerckléCSHARP10.0.7Success7,336+160.21 RP
May 5, 2026 14:34Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 14:30Victor MerckléVictor MerckléGOgo1.26.2Success40,681
May 5, 2026 14:05Victor MerckléVictor MerckléZIG0.16.0Success5,087+988.85 RP
May 5, 2026 13:59olliecrowolliecrowZIG0.15.1Success103,112
May 5, 2026 13:58Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 13:54olliecrowolliecrowZIG0.15.1Success55,574
May 5, 2026 13:53Victor MerckléVictor MerckléCPPg++14.2.0Success406,465
May 5, 2026 13:51Victor MerckléVictor MerckléCPPg++14.2.0Success407,000
May 5, 2026 13:49Victor MerckléVictor MerckléCPPg++14.2.0Success366,696
May 5, 2026 13:47Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 13:37Victor MerckléVictor MerckléCPPg++14.2.0Success366,593
May 5, 2026 13:35Victor MerckléVictor MerckléCPPg++14.2.0Success366,506
May 5, 2026 13:33Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 13:24Victor MerckléVictor MerckléCPPg++14.2.0Success366,662
May 5, 2026 13:18Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 13:16Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 13:14Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 12:57Victor MerckléVictor MerckléCPPg++14.2.0Success5,381
May 5, 2026 12:37Victor MerckléVictor MerckléCPPg++14.2.0Success39,894
May 5, 2026 12:32Victor MerckléVictor MerckléCPPg++14.2.0Success39,184
May 5, 2026 12:11Victor MerckléVictor MerckléCPPg++14.2.0Success366,977
May 5, 2026 12:01Victor MerckléVictor MerckléCPPg++14.2.0Success5,362
May 5, 2026 11:59Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 11:57Victor MerckléVictor MerckléCPPg++14.2.0Success4,950
May 5, 2026 11:55Victor MerckléVictor MerckléCPPg++14.2.0Success4,910+67.77 RP
May 5, 2026 11:53Victor MerckléVictor MerckléCPPg++14.2.0Success5,200
May 5, 2026 11:49Victor MerckléVictor MerckléCPPg++14.2.0Success5,737
May 5, 2026 11:41Victor MerckléVictor MerckléCPPg++14.2.0Success403,074
May 5, 2026 11:32Victor MerckléVictor MerckléCPPg++14.2.0Success5,082
May 5, 2026 11:30Victor MerckléVictor MerckléCPPg++14.2.0Success10,918
May 5, 2026 11:28Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 11:26Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 11:20Victor MerckléVictor MerckléCPPg++14.2.0Success5,079+317.64 RP
May 5, 2026 11:17Victor MerckléVictor MerckléCSHARP10.0.7Success8,313+1'013.49 RP
May 5, 2026 11:15Victor MerckléVictor MerckléCPPg++14.2.0Success9,693
May 5, 2026 10:49Victor MerckléVictor MerckléCSHARP10.0.7Error
May 5, 2026 10:41Victor MerckléVictor MerckléCPPg++14.2.0Error
May 5, 2026 10:37Victor MerckléVictor MerckléCPPg++14.2.0Success6,056+1'349.19 RP
May 5, 2026 10:31Victor MerckléVictor MerckléCPPg++14.2.0Success670,527
May 5, 2026 01:55olliecrowolliecrowCSHARP9.0.14Success110,841
May 5, 2026 00:57olliecrowolliecrowCSHARP9.0.14Success108,667
May 5, 2026 00:04olliecrowolliecrowCSHARP9.0.14Success112,674
May 4, 2026 22:35olliecrowolliecrowCPPclang++18.1.3Success92,210
May 4, 2026 22:23olliecrowolliecrowCSHARP9.0.15Success111,286
May 4, 2026 22:05olliecrowolliecrowCPPclang++18.1.3Success92,434
May 4, 2026 10:59Victor MerckléVictor MerckléCPPg++14.2.0Success361,610
May 4, 2026 10:21Victor MerckléVictor MerckléCPPg++14.2.0Success350,913
May 4, 2026 09:57Victor MerckléVictor MerckléCPPg++14.2.0Success331,848
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