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 7, 2023 08:21matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 7, 2023 05:09vsp3rvsp3rCPPg++9.3.0Error
Dec 7, 2023 05:06vsp3rvsp3rCPPg++9.3.0Error
Dec 7, 2023 05:03vsp3rvsp3rCPPg++9.3.0Error
Dec 7, 2023 05:02vsp3rvsp3rCPPg++9.3.0Error
Dec 7, 2023 05:01vsp3rvsp3rCPPg++9.3.0Success2,269,582
Dec 7, 2023 04:50vsp3rvsp3rCPPg++9.3.0Success2,260,412+4.42 RP
Dec 6, 2023 22:59BrrrrBrrrrCPPg++9.3.0Error
Dec 6, 2023 22:57BrrrrBrrrrCPPg++9.3.0Error
Dec 6, 2023 22:06BrrrrBrrrrCPPg++9.3.0Error
Dec 6, 2023 14:14lexlexCSHARP8.0.0Error
Dec 6, 2023 14:10lexlexCSHARP8.0.0Error
Dec 6, 2023 14:01lexlexCSHARP8.0.0Error
Dec 6, 2023 13:59lexlexCSHARP8.0.0Error
Dec 6, 2023 13:53lexlexCSHARP8.0.0Error
Dec 6, 2023 13:48lexlexCSHARP8.0.0Error
Dec 6, 2023 13:46lexlexCSHARP8.0.0Error
Dec 6, 2023 13:43lexlexCSHARP8.0.0Error
Dec 6, 2023 13:30lexlexCSHARP8.0.0Error
Dec 6, 2023 13:28lexlexCSHARP8.0.0Error
Dec 6, 2023 13:24lexlexCSHARP8.0.0Error
Dec 6, 2023 13:20lexlexCSHARP8.0.0Error
Dec 6, 2023 13:13lexlexCSHARP8.0.0Error
Dec 6, 2023 07:35lexlexCSHARP8.0.0Success622,643+13.10 RP
Dec 6, 2023 07:24lexlexCSHARP8.0.0Error
Dec 6, 2023 07:14lexlexCSHARP8.0.0Error
Dec 6, 2023 06:46lexlexCSHARP8.0.0Success3,414,897
Dec 6, 2023 06:44lexlexCSHARP8.0.0Success3,380,097+0.35 RP
Dec 6, 2023 06:42lexlexCSHARP8.0.0Error
Dec 6, 2023 06:40lexlexCSHARP8.0.0Error
Dec 6, 2023 06:38lexlexCSHARP8.0.0Success3,836,085+1.15 RP
Dec 6, 2023 06:33lexlexCSHARP8.0.0Error
Dec 6, 2023 06:30lexlexCSHARP8.0.0Error
Dec 6, 2023 06:29__RUSTrust-1.74.0Error
Dec 6, 2023 06:27lexlexCSHARP8.0.0Error
Dec 6, 2023 06:26__RUSTrust-1.74.0Error
Dec 6, 2023 06:23lexlexCSHARP8.0.0Error
Dec 6, 2023 06:23__RUSTrust-1.74.0Error
Dec 6, 2023 06:20lexlexCSHARP8.0.0Error
Dec 6, 2023 06:15lexlexCSHARP8.0.0Error
Dec 6, 2023 06:14__RUSTrust-1.74.0Error
Dec 6, 2023 06:08lexlexCSHARP8.0.0Error
Dec 6, 2023 06:08__RUSTrust-1.74.0Error
Dec 6, 2023 06:07lexlexCSHARP8.0.0Error
Dec 6, 2023 06:06lexlexCSHARP8.0.0Error
Dec 6, 2023 06:04lexlexCSHARP8.0.0Error
Dec 6, 2023 06:01lexlexCSHARP8.0.0Error
Dec 6, 2023 05:58lexlexCSHARP8.0.0Error
Dec 6, 2023 05:22lexlexCSHARP8.0.0Error
Dec 6, 2023 04:29lexlexCSHARP8.0.0Success6,842,101+1.46 RP
Dec 6, 2023 04:01lexlexCSHARP8.0.0Error
Dec 6, 2023 03:48lexlexCSHARP8.0.0Error
Dec 6, 2023 03:35lexlexCSHARP8.0.0Error
Dec 6, 2023 03:34lexlexCSHARP8.0.0Error
Dec 6, 2023 03:14lexlexCSHARP8.0.0Error
Dec 6, 2023 03:12lexlexCSHARP8.0.0Error
Dec 6, 2023 03:09lexlexCSHARP8.0.0Error
Dec 6, 2023 03:05lexlexCSHARP8.0.0Error
Dec 6, 2023 03:02lexlexCSHARP8.0.0Error
Dec 6, 2023 03:01lexlexCSHARP8.0.0Error
Dec 6, 2023 02:51lexlexCSHARP8.0.0Error
Dec 5, 2023 23:02AmirAmirCPPg++9.3.0Success2,513,299
Dec 5, 2023 23:02AmirAmirCPPg++9.3.0Error
Dec 5, 2023 22:56AmirAmirCPPg++9.3.0Success2,497,045+3.02 RP
Dec 5, 2023 22:52AmirAmirCPPg++9.3.0Success10,116,031+0.99 RP
Dec 5, 2023 19:10parastromparastromRUSTrust-1.74.0Error
Dec 5, 2023 17:34Yuriy LyfenkoYuriy LyfenkoGOgo1.17.5Success56,450+0.25 RP
Dec 5, 2023 17:17Yuriy LyfenkoYuriy LyfenkoGOgo1.17.5Error
Dec 5, 2023 17:12Yuriy LyfenkoYuriy LyfenkoGOgo1.21.4Success56,531+10.17 RP
Dec 5, 2023 17:12Yuriy LyfenkoYuriy LyfenkoGOgo1.21.4Success59,978+33.93 RP
Dec 5, 2023 17:04a14ua14uCPPg++9.3.0Success2,264,763+4.42 RP
Dec 5, 2023 16:51Yuriy LyfenkoYuriy LyfenkoGOgo1.21.4Error
Dec 5, 2023 16:51Yuriy LyfenkoYuriy LyfenkoGOgo1.21.4Success78,064
Dec 5, 2023 16:48paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:46paddypaddyGOgo1.21.4Success1,386,306+2.69 RP
Dec 5, 2023 16:45paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:43paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:40paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:35Yuriy LyfenkoYuriy LyfenkoGOgo1.21.4Success75,301+17.04 RP
Dec 5, 2023 16:35paddypaddyGOgo1.21.4Success2,212,718+4.52 RP
Dec 5, 2023 16:32paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:30paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:27paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:23paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:19paddypaddyGOgo1.21.4Error
Dec 5, 2023 16:18paddypaddyGOgo1.21.4Error
Dec 5, 2023 14:53Sergey StreminSergey StreminRUSTrust-1.59.0Success78,464
Dec 5, 2023 14:52Sergey StreminSergey StreminRUSTrust-1.59.0Success101,000
Dec 5, 2023 14:38Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:31Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:30Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Success39,677
Dec 5, 2023 14:29Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Success38,783+12.12 RP
Dec 5, 2023 14:28Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:27Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:25Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:24Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:22Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Success39,791
Dec 5, 2023 14:21Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:20Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error
Dec 5, 2023 14:19Yuriy LyfenkoYuriy LyfenkoCPPg++9.3.0Error