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 listOct 29, 2025 13:38CadovvlCadovvlScore: 60,631Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Sep 8, 2025 09:4860,34763,993,96333,001,0002,000,0002,506,752
2Aug 19, 2025 09:5960,35565,115,73634,006,0001,000,0002,461,696
3Oct 29, 2025 13:3860,38858,175,20634,025,0001,000,0002,490,368
4Aug 19, 2025 09:5960,41965,939,29934,042,0001,001,0002,416,640
5Sep 8, 2025 09:4860,51460,677,25133,093,0002,005,0002,281,472
6Aug 19, 2025 09:5960,53462,379,22735,110,00002,260,992
7Oct 29, 2025 13:3860,54561,214,79833,110,0002,006,0002,256,896
8Oct 29, 2025 13:3860,54766,405,83633,111,0002,006,0002,281,472
9Aug 19, 2025 09:5960,59062,078,94534,138,0001,004,0002,486,272
10Sep 8, 2025 09:5460,62167,025,27734,156,0001,004,0002,510,848
11Aug 19, 2025 09:5960,63159,860,57333,157,0002,009,0002,465,792
12Sep 8, 2025 09:4860,65562,964,61933,170,0002,010,0002,301,952
13Sep 8, 2025 09:5460,66665,483,83033,176,0002,010,0002,461,696
14Oct 29, 2025 13:3860,75561,134,60234,232,0001,006,0002,498,560
15Oct 29, 2025 13:3860,87259,535,41933,289,0002,017,0002,289,664
16Sep 8, 2025 09:5460,96956,472,91435,362,00002,281,472
17Aug 19, 2025 09:5961,04355,961,75335,405,00002,506,752
18Aug 19, 2025 09:5961,16458,375,52534,462,0001,013,0002,469,888
19Aug 19, 2025 09:5961,19360,146,21834,478,0001,014,0002,498,560
20Aug 19, 2025 09:5961,28156,321,78834,528,0001,015,0002,449,408
21Oct 29, 2025 13:3861,87664,291,83333,895,0001,993,0002,519,040