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 25, 2024 12:50E SequeiraE SequeiraCPPg++9.4.0Error
Mar 25, 2024 12:48E SequeiraE SequeiraCPPg++9.4.0Error
Mar 25, 2024 12:33E SequeiraE SequeiraCPPg++9.4.0Success2,650,100
Mar 25, 2024 12:32E SequeiraE SequeiraCPPg++9.4.0Success2,252,747
Mar 25, 2024 12:10E SequeiraE SequeiraCPPg++9.4.0Success2,246,905+4.45 RP
Mar 22, 2024 13:08Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,531,141
Mar 22, 2024 13:04Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success7,353,376
Mar 22, 2024 13:02Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,403,822
Mar 22, 2024 13:02Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,354,151
Mar 22, 2024 13:01Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Error
Mar 22, 2024 12:57Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,319,167+0.12 RP
Mar 22, 2024 12:56Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,339,798+2.27 RP
Mar 22, 2024 12:55Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Error
Mar 22, 2024 12:54Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Error
Mar 22, 2024 12:53Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Error
Mar 22, 2024 12:43Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success1,924,127+0.76 RP
Mar 22, 2024 12:42Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Error
Mar 22, 2024 12:37Zhao Wei LiewZhao Wei LiewCPPclang++10.0.0Error
Mar 22, 2024 12:19Zhao Wei LiewZhao Wei LiewCPPclang++10.0.0Success2,324,408
Mar 22, 2024 12:19Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success2,255,289
Mar 22, 2024 12:18Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success2,470,335
Mar 22, 2024 12:17Zhao Wei LiewZhao Wei LiewCPPg++9.4.0Success2,257,138+4.43 RP
Mar 10, 2024 23:18SrijithSrijithGOgo1.22.1Success3,284,663
Mar 10, 2024 04:52SrijithSrijithGOgo1.22.1Success1,374,333+7.28 RP
Mar 8, 2024 04:30Kailash GauthamKailash GauthamCPPg++9.4.0Success2,240,609+3.84 RP
Mar 7, 2024 13:24VoblissVoblissCPPg++9.4.0Error
Mar 7, 2024 13:22VoblissVoblissCPPg++9.4.0Success2,243,243+4.46 RP
Feb 20, 2024 19:29zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 19:23zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 19:17zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 18:22stdspstdspRUSTrust-1.76.0Success113,348
Feb 20, 2024 18:21stdspstdspRUSTrust-1.76.0Success111,904
Feb 20, 2024 18:19stdspstdspRUSTrust-1.76.0Success112,392
Feb 20, 2024 18:05zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 17:59stdspstdspRUSTrust-1.76.0Success111,846
Feb 20, 2024 17:56stdspstdspRUSTrust-1.76.0Success112,097
Feb 20, 2024 17:55zypehzypehRUSTrust-1.74.0Success164,365
Feb 20, 2024 17:54stdspstdspRUSTrust-1.76.0Success114,734
Feb 20, 2024 17:53zypehzypehRUSTrust-1.76.0Success154,685
Feb 20, 2024 17:53stdspstdspRUSTrust-1.76.0Success151,421
Feb 20, 2024 17:53stdspstdspRUSTrust-1.76.0Success113,413
Feb 20, 2024 17:51zypehzypehRUSTrust-1.76.0Error
Feb 20, 2024 17:49stdspstdspRUSTrust-1.76.0Success199,795
Feb 20, 2024 17:48stdspstdspRUSTrust-1.76.0Error
Feb 20, 2024 17:46stdspstdspRUSTrust-1.76.0Success109,726
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