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
Jan 19, 2024 20:46unopatientunopatientRUSTrust-1.75.0Success531,482
Jan 19, 2024 20:45unopatientunopatientRUSTrust-1.75.0Success529,840
Jan 19, 2024 20:23unopatientunopatientRUSTrust-1.75.0Success510,459+0.21 RP
Jan 19, 2024 10:57Tejas G.Tejas G.CPPg++9.4.0Error
Jan 18, 2024 05:51unopatientunopatientRUSTrust-1.75.0Success526,740
Jan 18, 2024 05:36unopatientunopatientRUSTrust-1.75.0Success515,989+0.01 RP
Jan 18, 2024 02:53unopatientunopatientRUSTrust-1.75.0Success525,310
Jan 18, 2024 01:54unopatientunopatientRUSTrust-1.75.0Success522,340
Jan 18, 2024 01:53unopatientunopatientRUSTrust-1.75.0Error
Jan 18, 2024 01:24unopatientunopatientRUSTrust-1.75.0Success516,265+0.01 RP
Jan 18, 2024 01:21unopatientunopatientRUSTrust-1.75.0Success517,946
Jan 18, 2024 01:20unopatientunopatientRUSTrust-1.75.0Success516,469+19.36 RP
Jan 16, 2024 02:23stdspstdspRUSTrust-1.75.0Success157,502+0.37 RP
Jan 16, 2024 02:18stdspstdspRUSTrust-1.75.0Success160,963
Jan 16, 2024 02:05stdspstdspRUSTrust-1.75.0Success158,976
Jan 16, 2024 02:05stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 02:04stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:56stdspstdspRUSTrust-1.75.0Success221,001
Jan 16, 2024 01:53stdspstdspRUSTrust-1.75.0Success158,417+0.08 RP
Jan 16, 2024 01:50stdspstdspRUSTrust-1.75.0Success159,575
Jan 16, 2024 01:15stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:11stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:07stdspstdspRUSTrust-1.75.0Success789,346
Jan 16, 2024 01:06stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 01:02stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:59stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:56stdspstdspRUSTrust-1.75.0Error
Jan 16, 2024 00:50stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 23:10stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 22:41stdspstdspRUSTrust-1.75.0Success791,729
Jan 15, 2024 22:38stdspstdspRUSTrust-1.75.0Success1,704,037
Jan 15, 2024 22:38stdspstdspRUSTrust-1.75.0Success1,770,860
Jan 15, 2024 22:32stdspstdspRUSTrust-1.75.0Success789,423
Jan 15, 2024 22:28stdspstdspRUSTrust-1.75.0Success795,171
Jan 15, 2024 22:25stdspstdspRUSTrust-1.75.0Success837,435
Jan 15, 2024 22:24stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 22:06stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 18:39stdspstdspRUSTrust-1.75.0Success163,236
Jan 15, 2024 18:30stdspstdspRUSTrust-1.75.0Success162,551
Jan 15, 2024 16:21stdspstdspRUSTrust-1.75.0Success167,436
Jan 15, 2024 16:21stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 15:10stdspstdspRUSTrust-1.75.0Success158,624+0.27 RP
Jan 15, 2024 14:49stdspstdspRUSTrust-1.75.0Success161,375
Jan 15, 2024 14:49stdspstdspRUSTrust-1.75.0Success161,472
Jan 15, 2024 14:48stdspstdspRUSTrust-1.75.0Success161,208
Jan 15, 2024 14:31stdspstdspRUSTrust-1.75.0Success166,846
Jan 15, 2024 14:24stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 14:07stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 14:01stdspstdspRUSTrust-1.75.0Success162,280
Jan 15, 2024 13:53stdspstdspRUSTrust-1.75.0Success165,382
Jan 15, 2024 13:52stdspstdspRUSTrust-1.75.0Success166,084
Jan 15, 2024 13:47stdspstdspRUSTrust-1.75.0Success163,609
Jan 15, 2024 13:46stdspstdspRUSTrust-1.75.0Error
Jan 15, 2024 13:40stdspstdspRUSTrust-1.75.0Error
Jan 14, 2024 21:02a14ua14uRUSTrust-1.75.0Error
Jan 14, 2024 21:01a14ua14uRUSTrust-1.75.0Error
Jan 14, 2024 15:09Tejas G.Tejas G.CPPg++9.4.0Success897,708
Jan 14, 2024 15:09Tejas G.Tejas G.CPPg++9.4.0Success895,862
Jan 14, 2024 15:09Tejas G.Tejas G.CPPg++9.4.0Success895,372
Jan 14, 2024 15:08Tejas G.Tejas G.CPPg++9.4.0Success893,991
Jan 14, 2024 15:06Tejas G.Tejas G.CPPg++9.4.0Success893,432+3.97 RP
Jan 14, 2024 15:02Tejas G.Tejas G.CPPg++9.4.0Success1,384,969+1.65 RP
Jan 14, 2024 13:34Tejas G.Tejas G.CPPg++9.4.0Error
Jan 14, 2024 13:33Tejas G.Tejas G.CPPg++9.4.0Error
Jan 14, 2024 12:55Tejas G.Tejas G.CPPg++9.4.0Error
Jan 14, 2024 12:53Tejas G.Tejas G.CPPg++9.4.0Error
Jan 14, 2024 08:40Tejas G.Tejas G.CPPg++9.4.0Error
Jan 14, 2024 06:50Kailash GauthamKailash GauthamCPPg++9.4.0Success15,937,123+0.63 RP
Jan 14, 2024 06:48Kailash GauthamKailash GauthamCPPg++9.4.0Error
Jan 14, 2024 06:08Kailash GauthamKailash GauthamCPPg++9.4.0Error
Jan 14, 2024 03:01Tejas G.Tejas G.CPPg++9.4.0Success1,795,128+1.20 RP
Jan 13, 2024 23:20stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 23:16stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 21:38stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 21:35stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 21:25stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 21:12stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 21:04stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 20:37yenw0dyenw0dGOgo1.21.3Success193,457
Jan 13, 2024 20:34yenw0dyenw0dGOgo1.21.3Error
Jan 13, 2024 20:32yenw0dyenw0dGOgo1.21.4Success150,943
Jan 13, 2024 20:30yenw0dyenw0dGOgo1.21.4Error
Jan 13, 2024 20:29yenw0dyenw0dGOgo1.21.4Success162,249
Jan 13, 2024 19:29stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 19:22stdspstdspRUSTrust-1.75.0Success2,683,348
Jan 13, 2024 19:21stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 19:18stdspstdspRUSTrust-1.75.0Success2,588,427
Jan 13, 2024 19:15stdspstdspRUSTrust-1.75.0Success2,477,138
Jan 13, 2024 19:14stdspstdspRUSTrust-1.75.0Success2,742,497
Jan 13, 2024 19:12stdspstdspRUSTrust-1.75.0Success2,622,446
Jan 13, 2024 19:10yenw0dyenw0dGOgo1.21.4Success230,953
Jan 13, 2024 19:06stdspstdspRUSTrust-1.75.0Success2,830,565
Jan 13, 2024 18:51stdspstdspRUSTrust-1.75.0Success2,573,388
Jan 13, 2024 18:51stdspstdspRUSTrust-1.75.0Success2,489,379
Jan 13, 2024 18:41stdspstdspRUSTrust-1.75.0Success2,463,538
Jan 13, 2024 18:26stdspstdspRUSTrust-1.75.0Success3,334,750
Jan 13, 2024 18:23stdspstdspRUSTrust-1.75.0Success2,534,413
Jan 13, 2024 18:13stdspstdspRUSTrust-1.75.0Success2,724,537
Jan 13, 2024 18:02stdspstdspRUSTrust-1.75.0Success4,561,879
Jan 13, 2024 17:59stdspstdspRUSTrust-1.75.0Success4,353,122