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
Jan 17, 2025 22:40alxmrcktralxmrcktrRUSTrust-1.84.0Success715,631
Jan 17, 2025 22:34alxmrcktralxmrcktrRUSTrust-1.84.0Success710,909+1.52 RP
Jan 17, 2025 22:29alxmrcktralxmrcktrRUSTrust-1.84.0Success797,081+12.55 RP
Jan 17, 2025 21:14Code HintsCode HintsRUSTrust-1.84.0Success1,887,233
Jan 17, 2025 19:44Code HintsCode HintsRUSTrust-1.84.0Success1,782,821+0.01 RP
Jan 17, 2025 19:39Code HintsCode HintsRUSTrust-1.84.0Success1,814,014
Jan 17, 2025 18:58alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 17, 2025 18:48Code HintsCode HintsRUSTrust-1.84.0Success1,785,191+0.15 RP
Jan 17, 2025 18:47alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 17, 2025 18:33Code HintsCode HintsRUSTrust-1.84.0Success1,835,362+0.52 RP
Jan 17, 2025 18:31alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 17, 2025 18:28alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 16, 2025 14:18alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 16, 2025 12:02alxmrcktralxmrcktrRUSTrust-1.84.0Error
Jan 16, 2025 08:07Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 08:05Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 08:03Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 08:01Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 07:47Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 07:45Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 07:44Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 07:39Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 16, 2025 02:19Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 02:14Code HintsCode HintsRUSTrust-1.78.0Success27,252,716
Jan 16, 2025 02:11Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 02:10Code HintsCode HintsRUSTrust-1.78.0Success3,579,984
Jan 16, 2025 02:00Code HintsCode HintsRUSTrust-1.78.0Success2,029,807+4.93 RP
Jan 16, 2025 01:54Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:34Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:33Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:26Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:25Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:22Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:18Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:16Code HintsCode HintsRUSTrust-1.78.0Error
Jan 16, 2025 01:07Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 22:10Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 21:25Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 17:54Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 17:46Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 17:44Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 17:43Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 17:32Code HintsCode HintsRUSTrust-1.78.0Error
Jan 15, 2025 11:37Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 15, 2025 08:55Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 15, 2025 08:52Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 15, 2025 08:40Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 15, 2025 08:30Haha ExpertHaha ExpertCSHARP9.0.1Success3,035,724
Jan 15, 2025 08:18Haha ExpertHaha ExpertCSHARP9.0.1Success4,190,731
Jan 15, 2025 06:44Haha ExpertHaha ExpertCSHARP9.0.1Error
Jan 15, 2025 01:34korey-skorey-sRUSTrust-1.84.0Success82,547+0.20 RP
Jan 15, 2025 01:33korey-skorey-sRUSTrust-1.84.0Success82,945
Jan 15, 2025 01:32korey-skorey-sRUSTrust-1.84.0Error
Jan 15, 2025 01:32korey-skorey-sRUSTrust-1.84.0Success83,097
Jan 15, 2025 01:27korey-skorey-sRUSTrust-1.84.0Success82,681+4.69 RP
Jan 14, 2025 23:42korey-skorey-sRUSTrust-1.84.0Success86,417
Jan 14, 2025 23:27korey-skorey-sRUSTrust-1.84.0Success86,678
Jan 14, 2025 23:13korey-skorey-sRUSTrust-1.84.0Success86,016+4.58 RP
Jan 14, 2025 22:56korey-skorey-sRUSTrust-1.84.0Error
Jan 14, 2025 22:54korey-skorey-sRUSTrust-1.84.0Error
Jan 14, 2025 22:37korey-skorey-sRUSTrust-1.84.0Success89,543+10.90 RP
Jan 12, 2025 14:32parastromparastromRUSTrust-1.84.0Error
Jan 12, 2025 14:30parastromparastromRUSTrust-1.84.0Error
Jan 12, 2025 14:29parastromparastromRUSTrust-1.84.0Error
Dec 31, 2024 20:22Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 20:20Louis PonetLouis PonetRUSTrust-1.83.0Success99,384+0.47 RP
Dec 31, 2024 20:10Louis PonetLouis PonetRUSTrust-1.83.0Success102,381
Dec 31, 2024 19:57Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:33Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:33Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:31Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:25Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:16Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:06Louis PonetLouis PonetRUSTrust-1.83.0Success99,850+5.34 RP
Dec 31, 2024 19:03Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 19:03Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 18:18Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 18:17Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 16:56Louis PonetLouis PonetRUSTrust-1.83.0Success106,209
Dec 31, 2024 16:56Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 16:55Louis PonetLouis PonetRUSTrust-1.83.0Success105,476+1.62 RP
Dec 31, 2024 16:28Louis PonetLouis PonetRUSTrust-1.83.0Success118,448
Dec 31, 2024 15:00Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 14:59Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 11:13Louis PonetLouis PonetRUSTrust-1.83.0Success108,998
Dec 31, 2024 11:13Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 11:13Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 11:12Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:51Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:44Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:41Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:40Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:39Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:36Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 10:33Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 09:57Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 09:56Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 09:55Louis PonetLouis PonetRUSTrust-1.83.0Success114,922
Dec 31, 2024 09:54Louis PonetLouis PonetRUSTrust-1.83.0Error
Dec 31, 2024 09:50Louis PonetLouis PonetRUSTrust-1.83.0Success107,998