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 listDec 16, 2025 23:32HattonuriHattonuriScore: 40,771Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 16, 2025 23:3237,45348,317,78621,723,00002,531,328
2Dec 16, 2025 23:3137,80046,571,83020,928,000996,0002,269,184
3Dec 16, 2025 23:3138,20347,543,32320,144,0002,014,0002,502,656
4Dec 16, 2025 23:3138,38348,708,15720,239,0002,023,0002,269,184
5Dec 16, 2025 23:3138,59344,788,64322,384,00002,269,184
6Dec 16, 2025 23:3238,75350,255,86122,477,00002,506,752
7Dec 16, 2025 23:3139,26244,129,67520,792,0001,980,0002,269,184
8Dec 16, 2025 23:3239,51248,658,17721,921,000996,0002,502,656
9Dec 16, 2025 23:3239,66048,988,83622,003,0001,000,0002,506,752
10Dec 16, 2025 23:3140,39544,581,96623,429,00002,498,560
11Dec 16, 2025 23:3240,42451,555,71622,427,0001,019,0002,531,328
12Dec 16, 2025 23:3240,60944,291,11823,553,00002,510,848
13Dec 16, 2025 23:3140,75048,033,99223,635,00002,527,232
14Dec 16, 2025 23:3140,77150,292,52923,647,00002,502,656
15Dec 16, 2025 23:3140,85947,683,00923,698,00002,322,432
16Dec 16, 2025 23:3140,91648,991,10422,700,0001,031,0002,498,560
17Dec 16, 2025 23:3141,19149,991,86223,891,00002,527,232
18Dec 16, 2025 23:3141,21749,042,70521,914,0001,992,0002,527,232
19Dec 16, 2025 23:3241,29746,214,49621,956,0001,996,0002,506,752
20Dec 16, 2025 23:3142,07446,984,56624,403,00002,531,328
21Dec 16, 2025 23:3142,27649,360,49624,520,00002,527,232
22Dec 16, 2025 23:3242,27943,997,68824,522,00002,494,464
23Dec 16, 2025 23:3242,41947,756,73424,603,00002,269,184
24Dec 16, 2025 23:3242,93346,172,37622,909,0001,992,0002,330,624
25Dec 16, 2025 23:3242,94350,860,76724,907,00002,527,232
26Dec 16, 2025 23:3143,10744,752,92924,002,0001,000,0002,527,232
27Dec 16, 2025 23:3243,41446,276,44624,173,0001,007,0002,502,656