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 23, 2025 04:16Josu San MartinJosu San MartinScore: 36,760Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 23, 2025 04:1734,51941,767,14019,020,0001,001,0002,281,472
2Dec 23, 2025 05:3534,75946,904,31319,152,0001,008,0002,457,600
3Dec 23, 2025 05:3535,02843,749,44519,301,0001,015,0002,469,888
4Dec 23, 2025 05:3535,31042,823,96220,480,00002,445,312
5Dec 23, 2025 04:1635,69748,191,89219,669,0001,035,0002,510,848
6Dec 23, 2025 04:1635,80550,231,75319,729,0001,038,0002,281,472
7Dec 23, 2025 05:3536,04347,269,45319,910,000995,0002,433,024
8Dec 23, 2025 04:1736,09144,399,08220,933,00002,379,776
9Dec 23, 2025 05:3536,31043,695,19419,055,0002,005,0002,375,680
10Dec 23, 2025 04:1636,37449,089,62319,088,0002,009,0002,404,352
11Dec 23, 2025 04:1636,38142,709,11019,092,0002,009,0002,314,240
12Dec 23, 2025 05:3536,39545,256,32119,099,0002,010,0002,326,528
13Dec 23, 2025 04:1636,76044,723,07421,321,00002,510,848
14Dec 23, 2025 05:3536,91250,668,08321,409,00002,347,008
15Dec 23, 2025 05:3537,05549,625,90220,469,0001,023,0002,424,832
16Dec 23, 2025 05:3537,07647,192,55521,504,00002,371,584
17Dec 23, 2025 05:3537,08345,911,11520,484,0001,024,0002,416,640
18Dec 23, 2025 04:1637,08843,824,79320,487,0001,024,0002,441,216
19Dec 23, 2025 04:1737,16444,930,58321,555,00002,387,968
20Dec 23, 2025 05:3537,24043,554,84620,571,0001,028,0002,371,584
21Dec 23, 2025 05:3537,32847,847,09321,650,00002,351,104
22Dec 23, 2025 04:1637,35247,593,47720,633,0001,031,0002,408,448
23Dec 23, 2025 04:1637,79345,328,62619,928,0001,992,0002,306,048
24Dec 23, 2025 04:1637,91647,945,21720,992,000999,0002,334,720