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 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
Aug 19, 2025 08:50CadovvlCadovvlCPPg++13.3.0Success60,836+0.42 RP
Aug 19, 2025 08:49CadovvlCadovvlCPPg++13.3.0Success62,188
Aug 19, 2025 08:46CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 07:35CadovvlCadovvlCPPg++13.3.0Success61,028
Aug 19, 2025 07:33CadovvlCadovvlCPPg++13.3.0Success61,909
Aug 19, 2025 07:33CadovvlCadovvlCPPg++13.3.0Success69,757
Aug 19, 2025 07:32CadovvlCadovvlCPPg++13.3.0Success61,000
Aug 19, 2025 07:31CadovvlCadovvlCPPg++13.3.0Success66,172
Aug 19, 2025 07:31CadovvlCadovvlCPPg++13.3.0Success64,836
Aug 19, 2025 07:30CadovvlCadovvlCPPg++13.3.0Success65,822
Aug 19, 2025 07:22CadovvlCadovvlCPPg++13.3.0Success60,990+2.50 RP
Aug 19, 2025 07:20CadovvlCadovvlCPPg++13.3.0Success66,633
Aug 19, 2025 07:16CadovvlCadovvlCPPg++13.3.0Success68,319
Aug 19, 2025 07:12CadovvlCadovvlCPPg++13.3.0Success61,936+0.65 RP
Aug 19, 2025 07:09CadovvlCadovvlCPPg++13.3.0Success62,188+0.48 RP
Aug 19, 2025 07:08CadovvlCadovvlCPPg++13.3.0Success81,209
Aug 19, 2025 07:07CadovvlCadovvlCPPg++13.3.0Success68,766
Aug 19, 2025 02:20CadovvlCadovvlCPPg++13.3.0Success63,112
Aug 19, 2025 02:18CadovvlCadovvlCPPg++13.3.0Success64,452
Aug 19, 2025 02:17CadovvlCadovvlCPPg++13.3.0Success62,374+2.95 RP
Aug 19, 2025 02:15CadovvlCadovvlCPPg++13.3.0Error
Aug 19, 2025 01:38CadovvlCadovvlCPPg++13.3.0Success64,076
Aug 19, 2025 01:36CadovvlCadovvlCPPg++13.3.0Success63,948
Aug 19, 2025 01:28CadovvlCadovvlCPPg++13.3.0Success63,543+0.35 RP
Aug 19, 2025 01:27CadovvlCadovvlCPPg++13.3.0Success65,533
Aug 19, 2025 01:27CadovvlCadovvlCPPg++13.3.0Success100,084
Aug 19, 2025 01:17CadovvlCadovvlCPPg++13.3.0Success64,752
Aug 19, 2025 01:16CadovvlCadovvlCPPg++13.3.0Success71,916
Aug 19, 2025 01:00CadovvlCadovvlCPPg++13.3.0Success65,574
Aug 19, 2025 00:58CadovvlCadovvlCPPg++13.3.0Success63,684+1.25 RP
Aug 19, 2025 00:57CadovvlCadovvlCPPg++13.3.0Success66,328
Aug 19, 2025 00:53CadovvlCadovvlCPPg++13.3.0Success64,426
Aug 19, 2025 00:52CadovvlCadovvlCPPg++13.3.0Success103,522