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
Jul 18, 2025 11:23CadovvlCadovvlCPPg++13.3.0Success78,667
Jul 18, 2025 11:21CadovvlCadovvlCPPg++13.3.0Success93,290
Jul 18, 2025 11:20CadovvlCadovvlCPPg++13.3.0Success93,869
Jul 18, 2025 11:18CadovvlCadovvlCPPg++13.3.0Success93,883
Jul 18, 2025 11:10CadovvlCadovvlCPPg++13.3.0Success92,943
Jul 18, 2025 11:09CadovvlCadovvlCPPg++13.3.0Success82,969
Jul 18, 2025 11:04CadovvlCadovvlCPPg++13.3.0Success98,557
Jul 18, 2025 11:03CadovvlCadovvlCPPg++13.3.0Success89,459
Jul 18, 2025 10:59CadovvlCadovvlCPPg++13.3.0Success79,591
Jul 18, 2025 10:57CadovvlCadovvlCPPg++13.3.0Success73,926
Jul 18, 2025 03:11stdspstdspRUSTrust-1.88.0Success353,829
Jul 18, 2025 02:59stdspstdspRUSTrust-1.88.0Success160,824
Jul 18, 2025 02:57stdspstdspRUSTrust-1.88.0Error
Jul 18, 2025 02:44stdspstdspRUSTrust-1.88.0Success162,531
Jul 18, 2025 02:43stdspstdspRUSTrust-1.88.0Success161,883
Jul 18, 2025 02:42stdspstdspRUSTrust-1.88.0Success162,722
Jul 18, 2025 02:41stdspstdspRUSTrust-1.88.0Success162,000
Jul 18, 2025 02:39stdspstdspRUSTrust-1.88.0Success162,674
Jul 18, 2025 02:37stdspstdspRUSTrust-1.88.0Success161,910
Jul 18, 2025 02:34stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:37stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:35stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:33stdspstdspRUSTrust-1.88.0Error
Jul 17, 2025 17:31stdspstdspRUSTrust-1.88.0Error
Jul 16, 2025 23:55TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 16:06TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 15:01TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 12:55TakopiTakopiRUSTrust-1.88.0Success152,795+0.19 RP
Jul 16, 2025 12:29TakopiTakopiRUSTrust-1.88.0Success153,236+36.12 RP
Jul 16, 2025 11:58TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:56TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:53TakopiTakopiRUSTrust-1.88.0Error
Jul 16, 2025 11:48TakopiTakopiRUSTrust-1.88.0Success343,183+0.74 RP
Jul 16, 2025 10:33TakopiTakopiRUSTrust-1.88.0Success352,152+28.40 RP
Jul 16, 2025 05:56CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:42CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:41CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:40CadovvlCadovvlCPPg++13.3.0Error
Jul 16, 2025 05:36CadovvlCadovvlCPPg++13.3.0Success73,947
Jul 16, 2025 05:34CadovvlCadovvlCPPg++13.3.0Success74,131
Jul 15, 2025 20:25Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:24Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:23Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:22Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:21Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:20Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success84,610
Jul 15, 2025 20:18Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success74,553+0.87 RP
Jul 15, 2025 20:07Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success76,424
Jul 15, 2025 20:07Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:06Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 15, 2025 20:06Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Error
Jul 15, 2025 19:59Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success76,412
Jul 15, 2025 19:59Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success75,041+2.96 RP
Jul 15, 2025 19:53Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success76,747+8.54 RP
Jul 15, 2025 19:49Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success87,726
Jul 15, 2025 19:49Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success86,328
Jul 15, 2025 13:51nolipolsupnolipolsupRUSTrust-1.88.0Success2,020,267
Jul 15, 2025 13:46nolipolsupnolipolsupRUSTrust-1.88.0Error
Jul 15, 2025 13:44nolipolsupnolipolsupRUSTrust-1.88.0Success523,822
Jul 15, 2025 13:43nolipolsupnolipolsupRUSTrust-1.88.0Success380,174
Jul 15, 2025 13:41nolipolsupnolipolsupRUSTrust-1.88.0Success379,634+0.37 RP
Jul 15, 2025 13:39nolipolsupnolipolsupRUSTrust-1.88.0Success385,093+2.80 RP
Jul 15, 2025 13:37nolipolsupnolipolsupRUSTrust-1.88.0Success431,612+23.17 RP
Jul 15, 2025 13:14CadovvlCadovvlCPPg++13.3.0Success73,984
Jul 15, 2025 13:12CadovvlCadovvlCPPg++13.3.0Success73,934
Jul 15, 2025 13:09CadovvlCadovvlCPPg++13.3.0Success73,778+0.34 RP
Jul 15, 2025 13:09nolipolsupnolipolsupRUSTrust-1.88.0Error
Jul 15, 2025 13:09CadovvlCadovvlCPPg++13.3.0Success74,019
Jul 15, 2025 13:07nolipolsupnolipolsupRUSTrust-1.88.0Error
Jul 15, 2025 13:06CadovvlCadovvlCPPg++13.3.0Success74,195
Jul 15, 2025 12:58CadovvlCadovvlCPPg++13.3.0Success73,995
Jul 15, 2025 12:55CadovvlCadovvlCPPg++13.3.0Success73,964+0.10 RP
Jul 15, 2025 12:52CadovvlCadovvlCPPg++13.3.0Success74,017+5.89 RP
Jul 15, 2025 12:49CadovvlCadovvlCPPg++13.3.0Success77,390+0.68 RP
Jul 15, 2025 12:48CadovvlCadovvlCPPg++13.3.0Error
Jul 15, 2025 12:31CadovvlCadovvlCPPg++13.3.0Success77,798+15.11 RP
Jul 15, 2025 12:27CadovvlCadovvlCPPg++13.3.0Success88,160+4.06 RP
Jul 15, 2025 12:20CadovvlCadovvlCPPg++13.3.0Success91,429+0.46 RP
Jul 15, 2025 11:49CadovvlCadovvlCPPg++13.3.0Success218,866
Jul 15, 2025 11:48CadovvlCadovvlCPPg++13.3.0Success200,919
Jul 15, 2025 11:47CadovvlCadovvlCPPclang++18.1.3Success143,893
Jul 15, 2025 11:47CadovvlCadovvlCPPg++13.3.0Success133,153
Jul 15, 2025 08:23CadovvlCadovvlCPPg++13.3.0Success91,819+27.10 RP
Jul 15, 2025 07:39CadovvlCadovvlCPPclang++18.1.3Success129,609
Jul 15, 2025 05:50CadovvlCadovvlCPPg++13.3.0Error
Jul 15, 2025 05:48CadovvlCadovvlCPPg++13.3.0Error
Jul 15, 2025 05:44CadovvlCadovvlCPPg++13.3.0Success122,238
Jul 15, 2025 05:43CadovvlCadovvlCPPg++13.3.0Error
Jul 15, 2025 05:42CadovvlCadovvlCPPg++13.3.0Error
Jul 15, 2025 05:32CadovvlCadovvlCPPg++13.3.0Success362,753
Jul 15, 2025 05:27CadovvlCadovvlCPPg++13.3.0Success122,236+0.20 RP
Jul 15, 2025 04:59CadovvlCadovvlCPPg++13.3.0Success122,531+36.27 RP
Jul 15, 2025 04:53CadovvlCadovvlCPPg++13.3.0Success220,545+1.24 RP
Jul 15, 2025 00:29BetaBetaCPPg++13.3.0Success1,289,993+7.75 RP
Jul 13, 2025 23:07Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success85,988
Jul 13, 2025 22:15Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success88,064
Jul 13, 2025 22:09Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success87,745
Jul 13, 2025 21:36Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success88,995
Jul 13, 2025 21:07Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 13, 2025 20:05Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success84,459