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
Oct 20, 2025 21:26benj252benj252RUSTrust-1.90.0Error
Oct 20, 2025 21:23benj252benj252RUSTrust-1.90.0Error
Oct 18, 2025 11:57korey-skorey-sRUSTrust-1.90.0Success83,338
Oct 18, 2025 11:56korey-skorey-sRUSTrust-1.90.0Success83,372
Oct 18, 2025 11:50korey-skorey-sRUSTrust-1.90.0Error
Oct 18, 2025 11:39korey-skorey-sRUSTrust-1.90.0Error
Oct 18, 2025 11:36korey-skorey-sRUSTrust-1.90.0Success84,633
Oct 15, 2025 22:21korey-skorey-sRUSTrust-1.90.0Success85,557
Oct 15, 2025 17:32Priyanshu GuptaPriyanshu GuptaCPPg++13.3.0Success1,298,410+7.70 RP
Oct 4, 2025 12:20parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 12:16parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 12:12parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 12:10parastromparastromRUSTrust-1.90.0Success104,710
Oct 4, 2025 12:05parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 11:42parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 11:39parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 11:35parastromparastromRUSTrust-1.90.0Error
Oct 4, 2025 11:17parastromparastromRUSTrust-1.90.0Success98,064
Oct 4, 2025 11:11parastromparastromRUSTrust-1.90.0Success105,921
Oct 4, 2025 10:34parastromparastromRUSTrust-1.90.0Success116,188
Oct 4, 2025 10:33parastromparastromRUSTrust-1.90.0Success115,414
Sep 30, 2025 16:40Wookyung JeongWookyung JeongCPPg++13.3.0Success51,541+1.89 RP
Sep 30, 2025 16:36Wookyung JeongWookyung JeongCPPg++13.3.0Success52,047+5.36 RP
Sep 30, 2025 16:25Wookyung JeongWookyung JeongCPPg++13.3.0Success53,541+3.59 RP
Sep 30, 2025 16:24Wookyung JeongWookyung JeongCPPg++13.3.0Success54,590+1.22 RP
Sep 30, 2025 16:21Wookyung JeongWookyung JeongCPPg++13.3.0Success55,412
Sep 30, 2025 16:09Wookyung JeongWookyung JeongCPPg++13.3.0Success54,995
Sep 30, 2025 15:19Wookyung JeongWookyung JeongCPPg++13.3.0Success57,553
Sep 30, 2025 15:01Wookyung JeongWookyung JeongCPPg++13.3.0Success58,393
Sep 30, 2025 14:28Wookyung JeongWookyung JeongCPPg++13.3.0Success55,772
Sep 30, 2025 09:58Jin KimJin KimCPPg++13.3.0Success323,047+3.66 RP
Sep 30, 2025 09:55Jin KimJin KimCPPg++13.3.0Success366,393+0.88 RP
Sep 30, 2025 09:51Jin KimJin KimCPPg++13.3.0Success378,548+18.69 RP
Sep 29, 2025 20:29Wookyung JeongWookyung JeongCPPg++13.3.0Success55,297
Sep 29, 2025 20:20Wookyung JeongWookyung JeongCPPg++13.3.0Success55,562
Sep 29, 2025 20:13Wookyung JeongWookyung JeongCPPg++13.3.0Success55,078
Sep 29, 2025 19:58Wookyung JeongWookyung JeongCPPg++13.3.0Success55,819
Sep 29, 2025 19:55Wookyung JeongWookyung JeongCPPg++13.3.0Success54,955+7.64 RP
Sep 29, 2025 19:55Wookyung JeongWookyung JeongCPPg++13.3.0Success57,728
Sep 29, 2025 19:54Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 29, 2025 19:47Wookyung JeongWookyung JeongCPPg++13.3.0Success57,364+1.79 RP
Sep 29, 2025 19:38Wookyung JeongWookyung JeongCPPg++13.3.0Success58,540
Sep 29, 2025 19:31Wookyung JeongWookyung JeongCPPg++13.3.0Success60,998
Sep 29, 2025 19:22Wookyung JeongWookyung JeongCPPg++13.3.0Success60,271
Sep 29, 2025 19:20Wookyung JeongWookyung JeongCPPg++13.3.0Success58,724
Sep 29, 2025 18:44Wookyung JeongWookyung JeongCPPg++13.3.0Success62,309
Sep 29, 2025 18:40Wookyung JeongWookyung JeongCPPg++13.3.0Success60,512
Sep 29, 2025 18:39Wookyung JeongWookyung JeongCPPg++13.3.0Success59,898
Sep 29, 2025 18:35Wookyung JeongWookyung JeongCPPg++13.3.0Success59,731
Sep 29, 2025 18:33Wookyung JeongWookyung JeongCPPg++13.3.0Success59,500
Sep 29, 2025 15:09Wookyung JeongWookyung JeongCPPg++13.3.0Success60,141
Sep 29, 2025 14:58Wookyung JeongWookyung JeongCPPg++13.3.0Success57,960+4.62 RP
Sep 29, 2025 14:54Wookyung JeongWookyung JeongCPPg++13.3.0Success60,578
Sep 29, 2025 14:51Wookyung JeongWookyung JeongCPPg++13.3.0Success60,138
Sep 29, 2025 14:36Wookyung JeongWookyung JeongCPPg++13.3.0Success63,584
Sep 29, 2025 08:39Wookyung JeongWookyung JeongCPPg++13.3.0Success60,153
Sep 29, 2025 08:36Wookyung JeongWookyung JeongCPPg++13.3.0Success60,310
Sep 29, 2025 08:30Wookyung JeongWookyung JeongCPPg++13.3.0Success60,686
Sep 29, 2025 08:27Wookyung JeongWookyung JeongCPPg++13.3.0Success60,721
Sep 29, 2025 08:26Wookyung JeongWookyung JeongCPPg++13.3.0Success62,029
Sep 29, 2025 08:17Wookyung JeongWookyung JeongCPPg++13.3.0Success59,555+4.65 RP
Sep 29, 2025 08:15Wookyung JeongWookyung JeongCPPg++13.3.0Success63,631
Sep 29, 2025 08:14Wookyung JeongWookyung JeongCPPg++13.3.0Success64,064
Sep 27, 2025 20:18Wookyung JeongWookyung JeongCPPg++13.3.0Success62,778
Sep 27, 2025 19:53Wookyung JeongWookyung JeongCPPg++13.3.0Success63,710
Sep 27, 2025 19:52Wookyung JeongWookyung JeongCPPg++13.3.0Success62,510
Sep 27, 2025 19:46Wookyung JeongWookyung JeongCPPg++13.3.0Success61,253+0.74 RP
Sep 27, 2025 19:36Wookyung JeongWookyung JeongCPPg++13.3.0Success61,533+3.66 RP
Sep 27, 2025 19:09Wookyung JeongWookyung JeongCPPg++13.3.0Success63,774
Sep 27, 2025 19:04Wookyung JeongWookyung JeongCPPg++13.3.0Success62,950+0.08 RP
Sep 27, 2025 18:42Wookyung JeongWookyung JeongCPPg++13.3.0Success62,981+0.42 RP
Sep 27, 2025 18:26Wookyung JeongWookyung JeongCPPg++13.3.0Success73,721
Sep 27, 2025 17:54Wookyung JeongWookyung JeongCPPg++13.3.0Success63,150+15.17 RP
Sep 27, 2025 17:30Wookyung JeongWookyung JeongCPPg++13.3.0Success69,843+1.99 RP
Sep 27, 2025 17:25Wookyung JeongWookyung JeongCPPg++13.3.0Success70,828+4.20 RP
Sep 27, 2025 17:23Wookyung JeongWookyung JeongCPPg++13.3.0Success73,912
Sep 27, 2025 17:22Wookyung JeongWookyung JeongCPPg++13.3.0Success73,000+1.84 RP
Sep 27, 2025 17:12Wookyung JeongWookyung JeongCPPg++13.3.0Success80,462
Sep 27, 2025 16:52Wookyung JeongWookyung JeongCPPg++13.3.0Success78,466
Sep 27, 2025 16:50Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 27, 2025 16:41Wookyung JeongWookyung JeongCPPg++13.3.0Success74,576
Sep 27, 2025 16:37Wookyung JeongWookyung JeongCPPg++13.3.0Success76,134
Sep 27, 2025 16:15Wookyung JeongWookyung JeongCPPg++13.3.0Success78,078
Sep 27, 2025 16:14Wookyung JeongWookyung JeongCPPg++13.3.0Success73,995+1.93 RP
Sep 27, 2025 16:08Wookyung JeongWookyung JeongCPPg++13.3.0Success75,066+1.00 RP
Sep 27, 2025 16:01Wookyung JeongWookyung JeongCPPg++13.3.0Success75,636+6.03 RP
Sep 27, 2025 15:55Wookyung JeongWookyung JeongCPPg++13.3.0Success79,905
Sep 27, 2025 15:52Wookyung JeongWookyung JeongCPPg++13.3.0Success102,157
Sep 27, 2025 15:42Wookyung JeongWookyung JeongCPPg++13.3.0Success84,276
Sep 27, 2025 15:20Wookyung JeongWookyung JeongCPPg++13.3.0Success83,055
Sep 27, 2025 15:10Wookyung JeongWookyung JeongCPPg++13.3.0Success80,522
Sep 27, 2025 15:02Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 27, 2025 15:02Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 27, 2025 15:01Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 27, 2025 15:00Wookyung JeongWookyung JeongCPPg++13.3.0Success79,248+2.39 RP
Sep 27, 2025 14:59Wookyung JeongWookyung JeongCPPg++13.3.0Error
Sep 27, 2025 14:58Wookyung JeongWookyung JeongCPPg++13.3.0Success84,291
Sep 27, 2025 14:51Wookyung JeongWookyung JeongCPPg++13.3.0Success87,712
Sep 27, 2025 14:51Wookyung JeongWookyung JeongCPPg++13.3.0Success83,817
Sep 27, 2025 14:46Wookyung JeongWookyung JeongCPPg++13.3.0Success87,191