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 20, 2025 14:35idatsyidatsyScore: 70,147Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 14:2767,21968,869,44037,988,000999,0002,285,568
2Dec 20, 2025 14:4767,74562,067,20838,285,0001,007,0002,277,376
3Dec 20, 2025 14:3567,75566,192,63937,283,0002,015,0002,285,568
4Dec 20, 2025 14:2767,91267,116,09639,389,00002,277,376
5Dec 20, 2025 14:2768,02864,125,03038,445,0001,011,0002,322,432
6Dec 20, 2025 14:3568,51664,564,92837,702,0002,037,0002,330,624
7Dec 20, 2025 14:2768,81263,829,55938,914,000997,0002,285,568
8Dec 20, 2025 14:2768,90566,761,63938,966,000999,0002,322,432
9Dec 20, 2025 14:2769,18461,820,94939,124,0001,003,0002,523,136
10Dec 20, 2025 14:4770,14768,883,99940,685,00002,269,184
11Dec 20, 2025 14:3570,28171,507,87039,744,0001,019,0002,289,664
12Dec 20, 2025 14:2770,54067,438,52137,920,0002,993,0002,269,184
13Dec 20, 2025 14:2770,55064,804,89840,919,00002,273,280
14Dec 20, 2025 14:3570,56067,863,94239,927,000998,0002,494,464
15Dec 20, 2025 14:4770,82462,842,57439,075,0002,003,0002,273,280
16Dec 20, 2025 14:3571,24564,828,17540,315,0001,007,0002,265,088
17Dec 20, 2025 14:2771,31465,462,67340,354,0001,008,0002,494,464
18Dec 20, 2025 14:3571,52266,525,89440,472,0001,011,0002,265,088