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
Mar 13, 2026 14:21olliecrowolliecrowCPPg++13.3.0Success6,031
Mar 13, 2026 14:19richardbranway1richardbranway1CPPg++13.3.0Success69,616+8.90 RP
Mar 13, 2026 14:19richardbranway1richardbranway1CPPg++13.3.0Success74,216+29.05 RP
Mar 13, 2026 14:11Dominique GarmierDominique GarmierCPPclang++18.1.3Success87,198+3.36 RP
Mar 13, 2026 14:10Dominique GarmierDominique GarmierCPPclang++18.1.3Error
Mar 13, 2026 14:10Dominique GarmierDominique GarmierCPPclang++18.1.3Error
Mar 13, 2026 14:08richardbranway1richardbranway1CPPg++13.3.0Success94,614+61.36 RP
Mar 13, 2026 14:06Dominique GarmierDominique GarmierCPPclang++18.1.3Success89,826+30.10 RP
Mar 13, 2026 14:04richardbranway1richardbranway1CPPg++13.3.0Success252,400
Mar 13, 2026 14:03Dominique GarmierDominique GarmierCPPg++13.3.0Error
Mar 13, 2026 14:03richardbranway1richardbranway1CPPg++13.3.0Success253,716
Mar 13, 2026 14:02richardbranway1richardbranway1CPPclang++18.1.3Success265,047
Mar 13, 2026 13:59richardbranway1richardbranway1CPPg++13.3.0Success252,690
Mar 13, 2026 13:58richardbranway1richardbranway1CPPg++13.3.0Success253,698
Mar 13, 2026 13:56Dominique GarmierDominique GarmierCPPclang++18.1.3Success123,112+25.07 RP
Mar 13, 2026 13:55Dominique GarmierDominique GarmierCPPclang++18.1.3Success181,041
Mar 13, 2026 13:54richardbranway1richardbranway1CPPg++13.3.0Success255,891
Mar 13, 2026 13:54Dominique GarmierDominique GarmierCPPclang++18.1.3Success178,067+22.39 RP
Mar 13, 2026 13:51richardbranway1richardbranway1CPPg++13.3.0Success268,214
Mar 13, 2026 13:42richardbranway1richardbranway1CPPg++13.3.0Success314,900
Mar 13, 2026 13:40olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:35olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:29richardbranway1richardbranway1CPPg++13.3.0Success225,555+0.08 RP
Mar 13, 2026 13:27olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:23richardbranway1richardbranway1CPPg++13.3.0Success231,488
Mar 13, 2026 13:16olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:11olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 13:11richardbranway1richardbranway1CPPg++13.3.0Success236,448
Mar 13, 2026 13:03richardbranway1richardbranway1CPPg++13.3.0Success225,962+0.07 RP
Mar 13, 2026 12:46richardbranway1richardbranway1CPPg++13.3.0Success226,343+2.23 RP
Mar 13, 2026 12:40richardbranway1richardbranway1CPPg++13.3.0Success238,366+0.54 RP
Mar 13, 2026 12:34richardbranway1richardbranway1CPPg++13.3.0Success334,591
Mar 13, 2026 12:27richardbranway1richardbranway1CPPg++13.3.0Success360,978
Mar 13, 2026 12:22richardbranway1richardbranway1CPPg++13.3.0Success363,948
Mar 13, 2026 12:19olliecrowolliecrowCPPg++13.3.0Success6,021
Mar 13, 2026 12:04richardbranway1richardbranway1CPPg++13.3.0Success241,478+13.41 RP
Mar 13, 2026 11:55olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 11:54Dominique GarmierDominique GarmierCPPg++13.3.0Success412,629
Mar 13, 2026 11:54richardbranway1richardbranway1CPPg++13.3.0Success357,121+20.22 RP
Mar 13, 2026 11:46Dominique GarmierDominique GarmierCPPg++13.3.0Success296,145+33.77 RP
Mar 13, 2026 11:39olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 11:27olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 11:22olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 11:09richardbranway1richardbranway1CPPg++13.3.0Success2,910,822
Mar 13, 2026 11:02richardbranway1richardbranway1CPPg++13.3.0Success2,863,431
Mar 13, 2026 10:53richardbranway1richardbranway1CPPg++13.3.0Success1,284,721+7.78 RP
Mar 13, 2026 10:50olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 10:45olliecrowolliecrowCPPg++13.3.0Success6,031
Mar 13, 2026 10:36olliecrowolliecrowCPPg++13.3.0Success5,860+18.44 RP
Mar 13, 2026 10:26olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 10:22olliecrowolliecrowCPPg++13.3.0Success6,055
Mar 13, 2026 10:13olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 09:33olliecrowolliecrowCPPg++13.3.0Success6,067
Mar 13, 2026 09:29olliecrowolliecrowCPPg++13.3.0Success6,012
Mar 13, 2026 09:22olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 09:17olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 09:12olliecrowolliecrowCPPg++13.3.0Success6,064
Mar 13, 2026 09:08olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 08:56olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 08:51olliecrowolliecrowCPPg++13.3.0Success5,969
Mar 13, 2026 08:21olliecrowolliecrowCPPg++13.3.0Success6,105
Mar 13, 2026 08:18olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 08:14olliecrowolliecrowCPPg++13.3.0Success6,347
Mar 13, 2026 08:01olliecrowolliecrowCPPg++13.3.0Success6,017
Mar 13, 2026 07:58olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 07:54olliecrowolliecrowCPPg++13.3.0Success6,174
Mar 13, 2026 07:50olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 07:42olliecrowolliecrowCPPg++13.3.0Success6,136
Mar 13, 2026 07:40olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 05:44olliecrowolliecrowCPPg++13.3.0Success9,257
Mar 13, 2026 05:39olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 05:20olliecrowolliecrowCPPg++13.3.0Success5,924+384.44 RP
Mar 13, 2026 05:07olliecrowolliecrowCPPg++13.3.0Success7,759
Mar 13, 2026 05:02olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:55olliecrowolliecrowCPPg++13.3.0Success7,976
Mar 13, 2026 04:55olliecrowolliecrowCPPg++13.3.0Success7,719
Mar 13, 2026 04:47olliecrowolliecrowCPPg++13.3.0Success7,671+195.09 RP
Mar 13, 2026 04:42olliecrowolliecrowCPPg++13.3.0Success9,021+15.87 RP
Mar 13, 2026 04:36olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:32olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:27olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:23olliecrowolliecrowCPPg++13.3.0Success9,152+9.35 RP
Mar 13, 2026 04:19olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:19olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:09olliecrowolliecrowCPPg++13.3.0Error
Mar 13, 2026 04:04olliecrowolliecrowCPPclang++18.1.3Success66,731
Mar 13, 2026 00:03Viktor VarbanovViktor VarbanovCPPg++13.3.0Success1,291,264+7.74 RP
Mar 12, 2026 20:46pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:45pseppsepCPPg++13.3.0Success114,529
Mar 12, 2026 20:44pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:40pseppsepCPPg++13.3.0Success111,621
Mar 12, 2026 20:38pseppsepCPPg++13.3.0Success146,447
Mar 12, 2026 20:36pseppsepCPPg++13.3.0Success275,819
Mar 12, 2026 20:32pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:31pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:29pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:27pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:26pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:25pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:23pseppsepCPPg++13.3.0Error