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 2, 2026 05:44RussellStrongRussellStrongScore: 108,466Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Jan 2, 2026 05:44106,74888,323,10959,917,0001,997,0002,359,296
2Jan 2, 2026 05:45107,01486,311,13459,065,0003,003,0002,359,296
3Jan 2, 2026 05:44107,34784,756,69661,257,0001,004,0002,289,664
4Jan 2, 2026 05:45107,52293,830,81460,384,0001,979,0002,355,200
5Jan 2, 2026 05:46107,64185,766,27661,426,0001,006,0002,347,008
6Jan 2, 2026 05:44107,71688,796,20361,468,0001,007,0002,355,200
7Jan 2, 2026 05:46107,86985,498,93662,564,00002,289,664
8Jan 2, 2026 05:45107,98186,798,11960,609,0002,020,0002,355,200
9Jan 2, 2026 05:46107,98690,538,16061,622,0001,010,0002,363,392
10Jan 2, 2026 05:45108,16292,248,39060,711,0002,023,0002,359,296
11Jan 2, 2026 05:46108,17491,787,12961,730,0001,011,0002,355,200
12Jan 2, 2026 05:44108,40791,799,31059,882,0002,994,0002,281,472
13Jan 2, 2026 05:45108,41486,034,09762,880,00002,473,984
14Jan 2, 2026 05:46108,46688,553,22160,913,0001,997,0002,289,664
15Jan 2, 2026 05:45108,55588,886,85861,963,000999,0002,355,200
16Jan 2, 2026 05:45108,58186,536,29960,978,0001,999,0002,347,008
17Jan 2, 2026 05:45108,60288,581,60861,990,000999,0002,289,664
18Jan 2, 2026 05:46108,60290,201,34461,990,000999,0002,355,200
19Jan 2, 2026 05:46108,62185,969,65758,000,0005,000,0002,359,296
20Jan 2, 2026 05:44108,67993,718,71161,033,0002,001,0002,355,200
21Jan 2, 2026 05:44108,87688,981,37659,139,0004,009,0002,289,664
22Jan 2, 2026 05:45108,95985,815,17060,187,0003,009,0002,289,664
23Jan 2, 2026 05:46109,06486,946,93860,245,0003,012,0002,355,200
24Jan 2, 2026 05:46109,13391,559,48462,293,0001,004,0002,289,664
25Jan 2, 2026 05:44109,17185,841,49662,314,0001,005,0002,289,664
26Jan 2, 2026 05:44109,20385,598,90661,328,0002,010,0002,359,296
27Jan 2, 2026 05:44109,36486,352,52861,418,0002,013,0002,359,296