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 listMar 2, 2026 21:22JuneLJuneLScore: 77,364Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 2, 2026 21:2367,16467,363,57834,960,0003,995,0002,375,680
2Mar 2, 2026 21:2268,66768,552,05033,853,0005,974,0002,318,336
3Mar 2, 2026 21:2268,75969,625,41834,895,0004,985,0002,269,184
4Mar 2, 2026 21:2269,18168,530,57235,110,0005,015,0002,326,528
5Mar 2, 2026 21:2371,42967,155,74836,377,0005,052,0002,498,560
6Mar 2, 2026 21:2372,20067,304,97737,888,0003,988,0002,473,984
7Mar 2, 2026 21:2373,94771,683,27037,902,0004,987,0002,310,144
8Mar 2, 2026 21:2274,35367,106,75838,111,0005,014,0002,478,080
9Mar 2, 2026 21:2374,82472,801,73637,343,0006,055,0002,449,408
10Mar 2, 2026 21:2276,43371,875,41037,279,0007,052,0002,465,792
11Mar 2, 2026 21:2377,36476,332,95540,883,0003,988,0002,510,848
12Mar 2, 2026 21:2277,37970,621,00839,894,0004,986,0002,318,336
13Mar 2, 2026 21:2279,75370,425,60941,229,0005,028,0002,510,848
14Mar 2, 2026 21:2379,86077,888,50340,278,0006,041,0002,420,736
15Mar 2, 2026 21:2380,15073,867,94340,424,0006,063,0002,306,048
16Mar 2, 2026 21:2380,27875,365,83440,488,0006,073,0002,457,600
17Mar 2, 2026 21:2281,00272,922,30140,984,0005,997,0002,363,392
18Mar 2, 2026 21:2281,02272,768,83940,994,0005,999,0002,514,944
19Mar 2, 2026 21:2282,06268,991,31742,533,0005,063,0002,506,752
20Mar 2, 2026 21:2283,31775,833,68542,284,0006,040,0002,478,080
21Mar 2, 2026 21:2287,82282,714,59944,945,0005,992,0002,441,216