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 listJul 18, 2025 15:12Alexey IlyukhovAlexey IlyukhovScore: 75,816Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jul 18, 2025 15:1370,92864,854,40339,132,0002,006,0002,260,992
2Jul 18, 2025 15:1271,50268,418,76340,460,0001,011,0002,269,184
3Jul 18, 2025 15:1271,51963,907,38939,458,0002,023,0002,404,352
4Jul 18, 2025 15:1371,80972,070,96341,649,00002,318,336
5Jul 18, 2025 15:1272,23866,207,81339,903,0001,995,0002,338,816
6Jul 18, 2025 15:1272,24866,938,91639,909,0001,995,0002,494,464
7Jul 18, 2025 15:1272,51468,576,25840,056,0002,002,0002,510,848
8Jul 18, 2025 15:1273,94374,229,79940,893,0001,994,0002,297,856
9Jul 18, 2025 15:1274,11775,974,42040,989,0001,999,0002,506,752
10Jul 18, 2025 15:1374,11773,173,60040,989,0001,999,0002,445,312
11Jul 18, 2025 15:1275,81672,201,17142,974,000999,0002,359,296
12Jul 18, 2025 15:1275,95769,706,48740,050,0004,005,0002,469,888
13Jul 18, 2025 15:1376,17172,959,89942,171,0002,008,0002,441,216
14Jul 18, 2025 15:1376,30767,840,85743,253,0001,005,0002,392,064
15Jul 18, 2025 15:1276,58471,575,11243,410,0001,009,0002,375,680
16Jul 18, 2025 15:1376,63469,314,88243,438,0001,010,0002,322,432
17Jul 18, 2025 15:1276,74765,955,04843,502,0001,011,0002,482,176
18Jul 18, 2025 15:1277,55567,805,48043,983,000999,0002,383,872
19Jul 18, 2025 15:1278,12971,161,15743,301,0002,014,0002,519,040
20Jul 18, 2025 15:1278,40967,553,75445,477,00002,334,720
21Jul 18, 2025 15:1279,25367,586,55443,969,0001,998,0002,289,664