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
Feb 20, 2024 17:45zypehzypehRUSTrust-1.76.0Success152,665
Feb 20, 2024 17:43stdspstdspRUSTrust-1.76.0Success151,114
Feb 20, 2024 17:42stdspstdspRUSTrust-1.76.0Success114,414
Feb 20, 2024 17:40stdspstdspRUSTrust-1.76.0Success112,430
Feb 20, 2024 17:38stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 17:35stdspstdspRUSTrust-1.76.0Success112,089
Feb 20, 2024 17:34stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 17:33stdspstdspRUSTrust-1.76.0Success106,867
Feb 20, 2024 17:32stdspstdspRUSTrust-1.76.0Success113,139
Feb 20, 2024 17:29stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 17:22zypehzypehRUSTrust-1.74.0Success113,359+88.22 RP
Feb 20, 2024 17:22zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 17:18stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 16:58stdspstdspRUSTrust-1.76.0Success105,940+3.97 RP
Feb 20, 2024 16:48stdspstdspRUSTrust-1.76.0Success112,136
Feb 20, 2024 16:47stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 16:47stdspstdspRUSTrust-1.76.0Success110,595+0.02 RP
Feb 20, 2024 16:44stdspstdspRUSTrust-1.76.0Error
Feb 19, 2024 12:250xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:250xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:240xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:230xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:200xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:180xUBN0xUBNRUSTrust-1.76.0Error
Feb 19, 2024 12:150xUBN0xUBNRUSTrust-1.76.0Error
Feb 18, 2024 19:33Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 19:32Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 19:02Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 18:56Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 18:48Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 18:32Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 18:07Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 17:55Marcus LeeMarcus LeeRUSTrust-1.76.0Success3,238,216
Feb 18, 2024 17:54Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 18, 2024 17:08Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 17:04Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 17:00Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 14:11Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 14:10Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 14:09Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 14:07Marcus LeeMarcus LeeCPPg++9.4.0Success841,923+7.59 RP
Feb 18, 2024 14:02Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 14:00Marcus LeeMarcus LeeRUSTrust-1.76.0Success3,343,470
Feb 18, 2024 13:59Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 13:57Marcus LeeMarcus LeeRUSTrust-1.76.0Error
Feb 18, 2024 12:52Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 12:47Marcus LeeMarcus LeeCPPg++9.4.0Success7,033,101
Feb 18, 2024 11:40Marcus LeeMarcus LeeCPPg++9.4.0Success6,974,195
Feb 18, 2024 05:23Tejas G.Tejas G.CPPg++9.4.0Error
Feb 18, 2024 05:21Tejas G.Tejas G.CPPg++9.4.0Error
Feb 18, 2024 05:20Tejas G.Tejas G.CPPg++9.4.0Error
Feb 18, 2024 05:17Tejas G.Tejas G.CPPg++9.4.0Error
Feb 18, 2024 05:15Tejas G.Tejas G.CPPg++9.4.0Error
Feb 18, 2024 04:51Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:50Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:48Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:45Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:41Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:40Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:36Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:32Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:30Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:28Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:28Marcus LeeMarcus LeeCPPg++9.4.0Error
Feb 18, 2024 04:24Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:22Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:22Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:16Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:12Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 04:12Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 03:36Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 03:31Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 00:40Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 18, 2024 00:40Marcus LeeMarcus LeeCPPclang++10.0.0Error
Feb 17, 2024 15:28parastromparastromRUSTrust-1.74.0Success93,031+32.42 RP
Feb 17, 2024 10:35Marcus LeeMarcus LeeCPPclang++10.0.0Success2,330,804+4.29 RP
Feb 17, 2024 06:57matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:54matsuoka-601matsuoka-601CPPclang++10.0.0Success33,036+92.01 RP
Feb 17, 2024 06:54matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:52matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:52matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:49matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:48matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:47matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:44matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:44matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:42matsuoka-601matsuoka-601CPPclang++10.0.0Error
Feb 17, 2024 06:20matsuoka-601matsuoka-601CPPg++9.4.0Error
Feb 17, 2024 06:19matsuoka-601matsuoka-601CPPg++9.4.0Error
Feb 17, 2024 06:19matsuoka-601matsuoka-601CPPg++9.4.0Success2,242,919
Feb 17, 2024 00:53Tejas G.Tejas G.CPPg++9.4.0Error
Feb 17, 2024 00:52Tejas G.Tejas G.CPPg++9.4.0Error
Feb 16, 2024 21:09Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 21:08Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Error
Feb 16, 2024 21:07Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success67,140
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,216
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,188
Feb 16, 2024 21:06Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success81,449
Feb 16, 2024 21:05Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success68,439
Feb 16, 2024 21:05Yuriy LyfenkoYuriy LyfenkoCPPg++9.4.0Success99,831