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 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
Mar 10, 2026 17:45Josu San MartinJosu San MartinCPPg++13.3.0Success3,800
Mar 10, 2026 17:44Josu San MartinJosu San MartinCPPg++13.3.0Success3,928
Mar 10, 2026 17:43Josu San MartinJosu San MartinCPPg++13.3.0Error
Mar 5, 2026 23:08Sergey SerebryakovSergey SerebryakovCPPg++13.3.0Success35,679+280.28 RP
Mar 3, 2026 01:00JuneLJuneLRUSTrust-1.93.1Success59,193
Mar 3, 2026 00:52JuneLJuneLRUSTrust-1.93.1Error
Mar 3, 2026 00:49JuneLJuneLRUSTrust-1.93.1Success46,048
Mar 3, 2026 00:40JuneLJuneLRUSTrust-1.93.1Success44,676+23.52 RP
Mar 3, 2026 00:35JuneLJuneLRUSTrust-1.93.1Success49,921+12.80 RP
Mar 3, 2026 00:35JuneLJuneLRUSTrust-1.93.1Success54,259
Mar 3, 2026 00:34JuneLJuneLRUSTrust-1.93.1Success58,083
Mar 2, 2026 22:25JuneLJuneLRUSTrust-1.93.1Error
Mar 2, 2026 22:23JuneLJuneLRUSTrust-1.93.1Success55,962
Mar 2, 2026 22:20JuneLJuneLRUSTrust-1.93.1Success146,562
Mar 2, 2026 22:16JuneLJuneLRUSTrust-1.93.1Success53,328+2.16 RP
Mar 2, 2026 22:12JuneLJuneLRUSTrust-1.93.1Success53,948+11.29 RP
Mar 2, 2026 22:08JuneLJuneLRUSTrust-1.93.1Success58,729
Mar 2, 2026 22:03JuneLJuneLRUSTrust-1.93.1Success57,448
Mar 2, 2026 21:56JuneLJuneLRUSTrust-1.93.1Success57,447+0.06 RP
Mar 2, 2026 21:54JuneLJuneLRUSTrust-1.93.1Success58,436
Mar 2, 2026 21:50JuneLJuneLRUSTrust-1.93.1Success83,997
Mar 2, 2026 21:45JuneLJuneLRUSTrust-1.93.1Success57,466+32.58 RP
Mar 2, 2026 21:41JuneLJuneLRUSTrust-1.93.1Success70,703+4.87 RP