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 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
Jan 13, 2024 17:49yenw0dyenw0dGOgo1.21.4Success230,821
Jan 13, 2024 17:49stdspstdspRUSTrust-1.75.0Error
Jan 13, 2024 17:40yenw0dyenw0dGOgo1.21.4Error
Jan 13, 2024 16:10Tejas G.Tejas G.CPPg++9.4.0Error
Jan 13, 2024 15:59Tejas G.Tejas G.CPPg++9.4.0Success2,303,355
Jan 13, 2024 15:57Tejas G.Tejas G.CPPg++9.4.0Success2,795,851
Jan 13, 2024 15:56Tejas G.Tejas G.CPPg++9.4.0Error
Jan 13, 2024 15:42Tejas G.Tejas G.CPPclang++10.0.0Error
Jan 13, 2024 15:39Tejas G.Tejas G.CPPclang++10.0.0Error
Jan 13, 2024 15:38Tejas G.Tejas G.CPPg++9.4.0Error
Jan 13, 2024 11:06Tejas G.Tejas G.CPPclang++10.0.0Success2,288,135+4.37 RP
Jan 13, 2024 10:59Tejas G.Tejas G.CPPclang++10.0.0Error
Jan 13, 2024 10:54Tejas G.Tejas G.CPPclang++10.0.0Error
Jan 13, 2024 10:50Tejas G.Tejas G.CPPclang++10.0.0Error
Jan 13, 2024 10:27Tejas G.Tejas G.CPPg++9.4.0Error
Jan 13, 2024 10:26Tejas G.Tejas G.CPPg++9.4.0Error
Jan 12, 2024 14:42Asahi NaruhayaAsahi NaruhayaRUSTrust-1.74.0Error
Jan 12, 2024 00:28cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:27cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:24cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:24cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:23cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:21cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:20cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:18cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:17cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:16cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:14cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:14cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:10cashkarucashkaruCPPclang++10.0.0Error
Jan 12, 2024 00:10cashkarucashkaruCPPg++9.4.0Error
Jan 11, 2024 23:03cashkarucashkaruCPPg++9.4.0Error
Jan 11, 2024 22:27cashkarucashkaruCPPg++9.4.0Error
Jan 11, 2024 22:03cashkarucashkaruCPPg++9.4.0Success2,255,464+4.43 RP
Jan 7, 2024 23:53stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:52Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success101,907
Jan 7, 2024 23:51stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:50stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:49stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:48stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:46Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success94,148
Jan 7, 2024 23:22stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:20stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:19stdspstdspRUSTrust-1.75.0Error