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 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
Mar 12, 2026 20:10pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:09pseppsepCPPg++13.3.0Success111,171+11.43 RP
Mar 12, 2026 20:07pseppsepCPPg++13.3.0Success577,312
Mar 12, 2026 20:06pseppsepCPPg++13.3.0Error
Mar 12, 2026 20:03pseppsepCPPg++13.3.0Success127,355+1.22 RP
Mar 12, 2026 20:00pseppsepCPPg++13.3.0Success163,036
Mar 12, 2026 19:58pseppsepCPPg++13.3.0Success323,233
Mar 12, 2026 19:54pseppsepCPPg++13.3.0Success181,617
Mar 12, 2026 19:52pseppsepCPPg++13.3.0Success129,366+17.02 RP
Mar 12, 2026 19:50pseppsepCPPg++13.3.0Success165,905+33.53 RP
Mar 12, 2026 19:47pseppsepCPPg++13.3.0Success43,755,553
Mar 12, 2026 19:36pseppsepCPPg++13.3.0Success373,847+26.75 RP
Mar 12, 2026 00:16KeefeKeefeCPPclang++18.1.3Success7,941
Mar 11, 2026 23:55KeefeKeefeCPPg++13.3.0Success6,543+1'390.86 RP
Mar 11, 2026 18:33KeefeKeefeCPPclang++18.1.3Success89,883
Mar 11, 2026 18:32KeefeKeefeCPPg++13.3.0Success78,078
Mar 11, 2026 17:46KeefeKeefeCPPg++13.3.0Error
Mar 11, 2026 17:07KeefeKeefeCPPg++13.3.0Error
Mar 11, 2026 16:58KeefeKeefeCPPg++13.3.0Success121,555
Mar 10, 2026 22:28Josu San MartinJosu San MartinCPPg++13.3.0Success3,810
Mar 10, 2026 22:27Josu San MartinJosu San MartinCPPg++13.3.0Success3,952
Mar 10, 2026 22:25Josu San MartinJosu San MartinCPPg++13.3.0Success4,367
Mar 10, 2026 20:01Josu San MartinJosu San MartinRUSTrust-1.92.0Success4,457
Mar 10, 2026 19:42Josu San MartinJosu San MartinRUSTrust-1.92.0Success4,348
Mar 10, 2026 19:22Josu San MartinJosu San MartinCPPg++13.3.0Success4,114
Mar 10, 2026 18:38Josu San MartinJosu San MartinCPPg++13.3.0Success4,245