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
Dec 9, 2023 06:34PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:33stdspstdspRUSTrust-1.74.0Success375,810
Dec 9, 2023 06:33PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:30stdspstdspRUSTrust-1.74.0Success917,279
Dec 9, 2023 06:28PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:27stdspstdspRUSTrust-1.74.0Success374,997+3.80 RP
Dec 9, 2023 06:25PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:19PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:14PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:12PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 06:12stdspstdspRUSTrust-1.74.0Success437,590
Dec 9, 2023 06:09stdspstdspRUSTrust-1.74.0Success459,299
Dec 9, 2023 06:08stdspstdspRUSTrust-1.74.0Success437,450
Dec 9, 2023 06:08PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:52PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:51PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:49PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:44PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:44stdspstdspRUSTrust-1.74.0Success437,356+0.01 RP
Dec 9, 2023 05:43stdspstdspRUSTrust-1.74.0Success440,588
Dec 9, 2023 05:42stdspstdspRUSTrust-1.74.0Success438,712
Dec 9, 2023 05:41stdspstdspRUSTrust-1.74.0Success437,729
Dec 9, 2023 05:39PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:37stdspstdspRUSTrust-1.74.0Success440,010
Dec 9, 2023 05:35stdspstdspRUSTrust-1.74.0Success437,544+0.14 RP
Dec 9, 2023 05:34stdspstdspRUSTrust-1.74.0Success442,209
Dec 9, 2023 05:34PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 05:32stdspstdspRUSTrust-1.74.0Success440,228+0.24 RP
Dec 9, 2023 05:29stdspstdspRUSTrust-1.74.0Success1,333,692
Dec 9, 2023 05:29stdspstdspRUSTrust-1.74.0Success444,915+0.34 RP
Dec 9, 2023 05:26stdspstdspRUSTrust-1.74.0Success451,721+4.18 RP
Dec 9, 2023 05:19stdspstdspRUSTrust-1.74.0Success1,522,552
Dec 9, 2023 05:18stdspstdspRUSTrust-1.74.0Error
Dec 9, 2023 05:18stdspstdspRUSTrust-1.74.0Success559,219
Dec 9, 2023 05:17stdspstdspRUSTrust-1.74.0Success556,919+0.02 RP
Dec 9, 2023 05:17stdspstdspRUSTrust-1.74.0Success557,827
Dec 9, 2023 05:17stdspstdspRUSTrust-1.74.0Success558,364
Dec 9, 2023 05:16stdspstdspRUSTrust-1.74.0Success557,454+0.22 RP
Dec 9, 2023 05:16stdspstdspRUSTrust-1.74.0Success565,638
Dec 9, 2023 05:15stdspstdspRUSTrust-1.74.0Success564,813
Dec 9, 2023 05:13stdspstdspRUSTrust-1.74.0Success564,251+0.02 RP
Dec 9, 2023 05:13stdspstdspRUSTrust-1.74.0Success565,045+0.08 RP
Dec 9, 2023 05:13stdspstdspRUSTrust-1.74.0Success567,763
Dec 9, 2023 05:11stdspstdspRUSTrust-1.74.0Success567,907+17.61 RP
Dec 9, 2023 04:55PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:42PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:34PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:28PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:25PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:23PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 9, 2023 04:00matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 9, 2023 00:17PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 8, 2023 18:34Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 17:51Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 17:30Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 16:40Brian PiccianoBrian PiccianoGOgo1.21.5Success934,445
Dec 8, 2023 16:39Brian PiccianoBrian PiccianoGOgo1.21.5Success898,310
Dec 8, 2023 16:23Brian PiccianoBrian PiccianoGOgo1.21.5Success897,331+3.97 RP
Dec 8, 2023 16:21Brian PiccianoBrian PiccianoGOgo1.21.5Success8,072,937
Dec 8, 2023 16:01Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 15:55Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 15:48Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 15:43matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:43matsuoka-601matsuoka-601CPPg++9.3.0Success48,237
Dec 8, 2023 15:37matsuoka-601matsuoka-601CPPclang++10.0.0Success40,458+21.88 RP
Dec 8, 2023 15:35matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:34Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 15:34matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:32matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:31Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 15:08matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:03matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 15:00matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 14:58matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 14:57matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 14:53matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 14:50matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 8, 2023 14:42Brian PiccianoBrian PiccianoGOgo1.21.5Success1,393,237+2.39 RP
Dec 8, 2023 14:30Brian PiccianoBrian PiccianoGOgo1.21.5Success2,088,001
Dec 8, 2023 14:28Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 14:24Brian PiccianoBrian PiccianoGOgo1.21.5Success2,089,148+4.79 RP
Dec 8, 2023 14:23Brian PiccianoBrian PiccianoGOgo1.21.5Error
Dec 8, 2023 13:22paddypaddyGOgo1.21.5Success1,606,195
Dec 8, 2023 13:20paddypaddyGOgo1.21.5Success1,614,905
Dec 8, 2023 12:43paddypaddyGOgo1.21.5Success1,400,028
Dec 8, 2023 12:40paddypaddyGOgo1.21.5Success3,228,387
Dec 8, 2023 12:33paddypaddyGOgo1.21.5Success3,595,558
Dec 8, 2023 12:32paddypaddyGOgo1.21.5Success3,480,888
Dec 7, 2023 23:19PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 7, 2023 21:19PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 7, 2023 21:16PiggyChan-2020PiggyChan-2020CPPclang++10.0.0Error
Dec 7, 2023 21:13PiggyChan-2020PiggyChan-2020CPPg++9.3.0Error
Dec 7, 2023 21:04PiggyChan-2020PiggyChan-2020CPPg++9.3.0Error
Dec 7, 2023 21:02PiggyChan-2020PiggyChan-2020CPPg++9.3.0Error
Dec 7, 2023 13:59BrrrrBrrrrCPPg++9.3.0Success2,270,938+4.40 RP
Dec 7, 2023 13:28lexlexCSHARP8.0.0Success3,004,219
Dec 7, 2023 13:14lexlexCSHARP8.0.0Error
Dec 7, 2023 13:13lexlexCSHARP8.0.0Error
Dec 7, 2023 13:08lexlexCSHARP8.0.0Error
Dec 7, 2023 13:04lexlexCSHARP8.0.0Error