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 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
Jul 13, 2025 19:42Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success86,855
Jul 13, 2025 19:20Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success82,129+4.40 RP
Jul 13, 2025 18:57Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success87,264
Jul 13, 2025 18:55Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success85,210+0.73 RP
Jul 13, 2025 18:54Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success5,067,814
Jul 13, 2025 18:54Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success3,012,647
Jul 13, 2025 17:46Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success85,740+2.82 RP
Jul 13, 2025 17:37Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success87,864+33.89 RP
Jul 13, 2025 17:13Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success129,195
Jul 13, 2025 16:59Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 13, 2025 16:56Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 13, 2025 16:56Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 13, 2025 16:55Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 13, 2025 16:55Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success126,033
Jul 13, 2025 16:55Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success125,124+1.20 RP
Jul 13, 2025 16:54Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success127,031+7.95 RP
Jul 13, 2025 16:51Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success141,810
Jul 13, 2025 16:50Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success146,269
Jul 13, 2025 16:47Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success144,979
Jul 13, 2025 16:43Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success141,305+3.01 RP
Jul 13, 2025 16:40Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 11, 2025 14:43CadovvlCadovvlCPPg++13.3.0Success228,003
Jul 11, 2025 14:24CadovvlCadovvlCPPclang++18.1.3Success1,002,241
Jul 11, 2025 14:24CadovvlCadovvlCPPg++13.3.0Success226,728+0.04 RP
Jul 11, 2025 14:04CadovvlCadovvlCPPg++13.3.0Success226,943+0.81 RP
Jul 11, 2025 13:39CadovvlCadovvlCPPg++13.3.0Success231,184+29.48 RP
Jul 11, 2025 13:39CadovvlCadovvlCPPclang++18.1.3Success1,009,219
Jul 11, 2025 11:54CadovvlCadovvlCPPclang++18.1.3Success725,957+0.20 RP
Jul 11, 2025 10:59CadovvlCadovvlCPPg++13.3.0Success736,436+0.94 RP
Jul 11, 2025 10:48CadovvlCadovvlCPPg++13.3.0Success791,198+12.64 RP
Jul 11, 2025 08:28CadovvlCadovvlCPPg++13.3.0Error
Jul 11, 2025 08:13CadovvlCadovvlCPPg++13.3.0Error
Jul 11, 2025 01:28Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success147,586+0.51 RP
Jul 11, 2025 01:22Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success149,386
Jul 11, 2025 01:20Alexey IlyukhovAlexey IlyukhovCPPclang++18.1.3Success163,976
Jul 11, 2025 01:20Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success148,712+28.42 RP
Jul 10, 2025 22:19Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Error
Jul 10, 2025 22:09Alexey IlyukhovAlexey IlyukhovCPPg++13.3.0Success257,586+38.82 RP