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
Sep 27, 2025 14:45Wookyung JeongWookyung JeongCPPg++13.3.0Success85,428
Sep 27, 2025 14:43Wookyung JeongWookyung JeongCPPg++13.3.0Success80,776+5.12 RP
Sep 27, 2025 14:42Wookyung JeongWookyung JeongCPPg++13.3.0Success88,657
Sep 27, 2025 14:36Wookyung JeongWookyung JeongCPPg++13.3.0Success86,928
Sep 27, 2025 14:27Wookyung JeongWookyung JeongCPPg++13.3.0Success84,262+3.22 RP
Sep 27, 2025 14:16Wookyung JeongWookyung JeongCPPg++13.3.0Success86,609+0.54 RP
Sep 27, 2025 12:01Jin KimJin KimCPPg++13.3.0Error
Sep 27, 2025 11:58Wookyung JeongWookyung JeongCPPg++13.3.0Success88,012
Sep 27, 2025 11:58Wookyung JeongWookyung JeongCPPg++13.3.0Success89,900
Sep 27, 2025 11:57Wookyung JeongWookyung JeongCPPg++13.3.0Success87,019+0.86 RP
Sep 27, 2025 11:36Wookyung JeongWookyung JeongCPPg++13.3.0Success87,678+6.46 RP
Sep 27, 2025 11:30Wookyung JeongWookyung JeongCPPg++13.3.0Success92,941+107.60 RP
Sep 27, 2025 10:41Jin KimJin KimCPPg++13.3.0Success1,294,028+7.73 RP
Sep 18, 2025 00:07MylesMylesCPPg++13.3.0Error
Sep 18, 2025 00:06MylesMylesCPPg++13.3.0Error
Sep 14, 2025 17:57caandocaandoCPPg++13.3.0Error
Sep 14, 2025 17:51caandocaandoCPPg++13.3.0Error
Sep 13, 2025 23:59caandocaandoCPPg++13.3.0Success117,833
Sep 13, 2025 23:37caandocaandoCPPg++13.3.0Error
Sep 13, 2025 23:31caandocaandoCPPg++13.3.0Success311,505
Sep 13, 2025 23:18caandocaandoCPPg++13.3.0Success102,291
Sep 13, 2025 23:18caandocaandoCPPg++13.3.0Success101,697
Sep 13, 2025 21:27caandocaandoCPPg++13.3.0Success96,078
Sep 13, 2025 21:21caandocaandoCPPg++13.3.0Success100,957
Sep 13, 2025 21:07caandocaandoCPPg++13.3.0Success95,284+5.50 RP
Sep 13, 2025 21:00caandocaandoCPPg++13.3.0Error
Sep 13, 2025 20:54caandocaandoCPPg++13.3.0Success102,821
Sep 13, 2025 20:48caandocaandoCPPg++13.3.0Success101,562
Sep 13, 2025 20:45caandocaandoCPPg++13.3.0Success117,786
Sep 13, 2025 20:38caandocaandoCPPg++13.3.0Success100,555+4.57 RP
Sep 13, 2025 20:36caandocaandoCPPg++13.3.0Success111,457
Sep 13, 2025 20:20caandocaandoCPPg++13.3.0Error
Sep 13, 2025 20:12caandocaandoCPPg++13.3.0Success105,395+1.38 RP
Sep 13, 2025 20:03caandocaandoCPPg++13.3.0Success107,967
Sep 13, 2025 20:00caandocaandoCPPg++13.3.0Error
Sep 13, 2025 19:51caandocaandoCPPg++13.3.0Error
Sep 13, 2025 19:47caandocaandoCPPg++13.3.0Success106,945+6.86 RP
Sep 11, 2025 16:51caandocaandoCPPg++13.3.0Success115,417+0.79 RP
Sep 11, 2025 16:50caandocaandoCPPg++13.3.0Success116,719
Sep 11, 2025 16:47caandocaandoCPPg++13.3.0Success116,481+1.42 RP
Sep 11, 2025 16:42caandocaandoCPPg++13.3.0Success148,619
Sep 11, 2025 16:41caandocaandoCPPg++13.3.0Success155,240
Sep 11, 2025 16:38caandocaandoCPPg++13.3.0Success143,884
Sep 11, 2025 16:30caandocaandoCPPg++13.3.0Success118,440+12.95 RP
Sep 11, 2025 16:22caandocaandoCPPg++13.3.0Success139,905+31.73 RP
Sep 11, 2025 16:20caandocaandoCPPg++13.3.0Success251,597+0.43 RP
Sep 11, 2025 16:15caandocaandoCPPg++13.3.0Success258,700
Sep 11, 2025 16:10caandocaandoCPPg++13.3.0Success254,972
Sep 11, 2025 16:06caandocaandoCPPg++13.3.0Success254,331+6.52 RP
Sep 11, 2025 16:02caandocaandoCPPg++13.3.0Success332,403
Sep 11, 2025 16:01caandocaandoCPPg++13.3.0Success340,329
Sep 11, 2025 15:56caandocaandoCPPg++13.3.0Success304,869+12.70 RP
Sep 11, 2025 15:54caandocaandoCPPg++13.3.0Success497,478+9.42 RP
Sep 11, 2025 15:27caandocaandoCPPg++13.3.0Success936,519+10.68 RP
Sep 9, 2025 20:04Jacqueline NabagloJacqueline NabagloCPPclang++18.1.3Success668,957
Sep 9, 2025 20:02Jacqueline NabagloJacqueline NabagloCPPclang++18.1.3Success665,455
Sep 9, 2025 20:02Jacqueline NabagloJacqueline NabagloCPPclang++18.1.3Success829,038
Sep 9, 2025 20:01Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Success659,934
Sep 9, 2025 20:00Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Error
Sep 9, 2025 16:56Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Error
Sep 9, 2025 16:48Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Success656,359+7.50 RP
Sep 9, 2025 04:29Fernando Clark del ValleFernando Clark del ValleRUSTrust-1.89.0Success127,564
Sep 9, 2025 04:00Fernando Clark del ValleFernando Clark del ValleRUSTrust-1.89.0Success131,040
Sep 9, 2025 03:40Fernando Clark del ValleFernando Clark del ValleRUSTrust-1.89.0Success127,059+78.70 RP
Sep 8, 2025 21:03Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Success1,292,390+0.02 RP
Sep 8, 2025 21:02Jacqueline NabagloJacqueline NabagloCPPg++13.3.0Success1,295,119+7.72 RP
Sep 8, 2025 16:15yenw0dyenw0dRUSTrust-1.85.1Success66,133+5.11 RP
Sep 8, 2025 16:13yenw0dyenw0dRUSTrust-1.85.1Error
Sep 8, 2025 16:11yenw0dyenw0dRUSTrust-1.85.1Error
Sep 8, 2025 14:59Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success68,779+3.54 RP
Sep 8, 2025 14:51Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success70,774
Sep 8, 2025 14:47Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success72,221
Sep 8, 2025 14:05Pavel SamyginPavel SamyginCPPclang++18.1.3Success1,280,114
Sep 8, 2025 09:53CadovvlCadovvlCPPg++13.3.0Success63,838
Sep 8, 2025 09:48CadovvlCadovvlCPPg++13.3.0Error
Sep 7, 2025 22:05Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success70,497+0.02 RP
Sep 7, 2025 22:05Daniel LubarovDaniel LubarovRUSTrust-1.89.0Error
Sep 7, 2025 22:04Daniel LubarovDaniel LubarovRUSTrust-1.89.0Error
Sep 7, 2025 22:04Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success70,509+3.77 RP
Sep 7, 2025 22:03Daniel LubarovDaniel LubarovRUSTrust-1.89.0Error
Sep 7, 2025 22:00Daniel LubarovDaniel LubarovRUSTrust-1.89.0Error
Sep 7, 2025 21:23Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success74,569
Sep 7, 2025 21:16Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success72,434+123.39 RP
Sep 7, 2025 20:34Daniel LubarovDaniel LubarovRUSTrust-1.89.0Success681,938+14.66 RP
Sep 7, 2025 17:03Pavel SamyginPavel SamyginCPPclang++18.1.3Success730,924+13.68 RP
Sep 7, 2025 14:19MylesMylesCPPg++13.3.0Success41,838
Sep 6, 2025 19:20MylesMylesCPPg++13.3.0Success44,336
Sep 6, 2025 16:32Daniel LubarovDaniel LubarovRUSTrust-1.89.0Error
Sep 6, 2025 14:14MylesMylesCPPg++13.3.0Success44,724
Sep 6, 2025 13:54MylesMylesCPPg++13.3.0Success43,053
Sep 6, 2025 13:53MylesMylesCPPg++13.3.0Error
Sep 6, 2025 10:08Pavel SamyginPavel SamyginCPPclang++18.1.3Error
Sep 6, 2025 01:32Eli RiggsEli RiggsRUSTrust-1.89.0Success74,472+5.60 RP
Sep 6, 2025 01:18Eli RiggsEli RiggsRUSTrust-1.89.0Success77,712+46.02 RP
Sep 5, 2025 23:38Eli RiggsEli RiggsRUSTrust-1.89.0Success120,972+71.44 RP
Sep 5, 2025 04:40MylesMylesCPPg++13.3.0Success36,659+13.94 RP
Sep 5, 2025 00:05MylesMylesCPPg++13.3.0Success38,634+177.46 RP
Sep 4, 2025 18:38Eli RiggsEli RiggsRUSTrust-1.89.0Success890,626+3.49 RP
Sep 4, 2025 16:21Eli RiggsEli RiggsCPPg++13.3.0Success1,292,712+7.74 RP
Aug 21, 2025 11:28CadovvlCadovvlCPPg++13.3.0Success73,612