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.

Back to listJan 20, 2025 16:40alxmrcktralxmrcktrScore: 134,522Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 21, 2025 00:04133,50997,220,93576,430,0001,005,0002,269,184
2Jan 21, 2025 00:04133,81098,171,98676,603,0001,007,0002,535,424
3Jan 21, 2025 00:04133,962106,427,47676,689,0001,009,0002,531,328
4Jan 21, 2025 00:04134,243105,081,17674,867,0002,994,0002,535,424
5Jan 21, 2025 00:04134,360103,745,58375,931,0001,998,0002,285,568
6Jan 20, 2025 16:40134,405100,749,99274,957,0002,998,0002,535,424
7Jan 21, 2025 00:04134,522104,149,77473,022,0005,001,0002,535,424
8Jan 21, 2025 00:04134,566106,041,08676,047,0002,001,0002,281,472
9Jan 21, 2025 00:04134,857104,702,80777,215,0001,002,0002,535,424
10Jan 20, 2025 16:40135,031104,282,30875,306,0003,012,0002,269,184
11Jan 21, 2025 00:04136,076107,205,58376,926,0001,998,0002,527,232
12Jan 20, 2025 16:40137,981111,726,64777,028,0003,001,0002,535,424