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 listFeb 19, 2026 02:54Josu San MartinJosu San MartinScore: 3,719Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 19, 2026 02:573,55521,917,51602,062,0002,330,624
2Feb 19, 2026 02:523,57917,450,3331,038,0001,038,0002,502,656
3Feb 19, 2026 03:023,61018,264,2161,047,0001,047,0002,318,336
4Feb 19, 2026 02:543,65518,881,62002,120,0002,297,856
5Feb 19, 2026 03:013,66220,530,0211,062,0001,062,0002,306,048
6Feb 19, 2026 03:023,68320,597,7711,068,0001,068,0002,277,376
7Feb 19, 2026 02:573,70318,231,1341,074,0001,074,0002,273,280
8Feb 19, 2026 03:013,71921,769,10302,157,0002,478,080
9Feb 19, 2026 02:523,72118,025,95502,158,0002,445,312
10Feb 19, 2026 03:013,76223,448,2721,091,0001,091,0002,473,984
11Feb 19, 2026 02:573,79017,884,1931,099,0001,099,0002,514,944
12Feb 19, 2026 02:543,81015,699,8111,105,0001,105,0002,502,656
13Feb 19, 2026 02:543,82117,906,5121,108,0001,108,0002,441,216
14Feb 19, 2026 03:023,83425,422,8741,112,0001,112,0002,473,984
15Feb 19, 2026 02:523,83822,181,38802,226,0002,457,600