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 27, 2026 23:03Samuel VanderwaalSamuel VanderwaalScore: 60,157Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 27, 2026 23:0357,06256,445,93231,091,0002,005,0002,412,544
2Feb 27, 2026 23:0357,97257,763,13932,606,0001,018,0002,363,392
3Feb 27, 2026 22:3358,96959,851,81731,185,0003,017,0002,506,752
4Feb 27, 2026 22:3359,33858,321,45831,380,0003,036,0002,510,848
5Feb 27, 2026 23:0359,35767,487,96234,427,00002,322,432
6Feb 27, 2026 22:3359,40557,204,24533,442,0001,013,0002,498,560
7Feb 27, 2026 22:3359,70058,684,71633,608,0001,018,0002,371,584
8Feb 27, 2026 23:0359,77165,884,99331,609,0003,058,0002,494,464
9Feb 27, 2026 22:3360,15060,719,46333,861,0001,026,0002,461,696
10Feb 27, 2026 22:3360,15760,115,92430,904,0003,987,0002,318,336
11Feb 27, 2026 23:0360,60357,863,15832,138,0003,012,0002,514,944
12Feb 27, 2026 22:3360,60757,359,41531,135,0004,017,0002,293,760
13Feb 27, 2026 23:0360,65357,781,92934,174,0001,005,0002,338,816
14Feb 27, 2026 23:0360,70357,454,50933,197,0002,011,0002,457,600
15Feb 27, 2026 23:0361,34359,125,27733,546,0002,033,0002,273,280
16Feb 27, 2026 22:3361,43162,907,37433,594,0002,036,0002,322,432
17Feb 27, 2026 22:3361,87961,449,32233,897,0001,993,0002,363,392
18Feb 27, 2026 23:0362,37161,668,16134,166,0002,009,0002,338,816