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 1, 2026 17:30yenw0dyenw0dScore: 12,593Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 1, 2026 17:3311,91924,279,4511,975,0004,938,0002,285,568
2Jan 1, 2026 17:3111,99324,004,3641,987,0004,969,0002,465,792
3Jan 1, 2026 17:3012,06225,396,6621,999,0004,997,0002,285,568
4Jan 1, 2026 17:3012,09325,896,7652,004,0005,010,0002,416,640
5Jan 1, 2026 17:3212,09522,417,6952,004,0005,011,0002,379,776
6Jan 1, 2026 17:2912,16021,430,9982,015,0005,038,0002,383,872
7Jan 1, 2026 17:3212,21724,049,7393,037,0004,049,0002,494,464
8Jan 1, 2026 17:3312,39726,871,2693,081,0004,109,0002,289,664
9Jan 1, 2026 17:3012,45026,702,7612,063,0005,158,0002,428,928
10Jan 1, 2026 17:3112,48827,873,8673,104,0004,139,0002,523,136
11Jan 1, 2026 17:2912,59328,371,0562,087,0005,217,0002,297,856
12Jan 1, 2026 17:3212,62129,490,6373,137,0004,183,0002,473,984
13Jan 1, 2026 17:3212,63124,018,2142,093,0005,233,0002,523,136
14Jan 1, 2026 17:3212,69826,883,4881,052,0006,313,0002,342,912
15Jan 1, 2026 17:3312,78128,104,0251,059,0006,354,0002,306,048
16Jan 1, 2026 17:2912,85322,465,1963,195,0004,260,0002,363,392
17Jan 1, 2026 17:3113,04322,299,5231,080,0006,485,0002,514,944
18Jan 1, 2026 17:3013,04324,739,7962,161,0005,404,0002,473,984
19Jan 1, 2026 17:3013,25227,504,0431,098,0006,588,0002,498,560
20Jan 1, 2026 17:3213,69023,344,8593,970,0003,970,0002,269,184
21Jan 1, 2026 17:3013,73323,581,5251,991,0005,974,0002,514,944