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 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
Jan 7, 2024 23:16stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:14stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:11stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:02stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 23:01stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:58stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:54stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:53stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:47stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:45stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:42stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:25stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:24stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:20stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:19stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 22:14stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:43stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:41stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:39stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:37stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:32stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 21:28stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:16stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:08stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:06stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:02stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:01stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 20:00stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:59stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:37stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:24stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 19:19stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 18:30stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 18:04stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:54stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:17stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:11stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:09stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:08stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:06stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 17:03stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 16:32stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 16:21stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:57stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:53stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:34stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:23stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:20stdspstdspRUSTrust-1.75.0Success161,378
Jan 7, 2024 15:18stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:07stdspstdspRUSTrust-1.75.0Error
Jan 7, 2024 15:03stdspstdspRUSTrust-1.75.0Error
Jan 3, 2024 22:35Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Success90,336+13.60 RP
Jan 3, 2024 22:19Yurkov AlekseyYurkov AlekseyCPPg++9.4.0Error