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 12, 2023 08:38matsuoka-601matsuoka-601Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 12, 2023 08:4300000Error: No answer to stdout. Died or killed?stderr
2Dec 12, 2023 08:4000000Error: No answer to stdout. Died or killed?stderr
3Dec 12, 2023 08:4100000Error: No answer to stdout. Died or killed?stderr
4Dec 12, 2023 08:4200000Error: No answer to stdout. Died or killed?stderr
5Dec 12, 2023 08:4200000Error: No answer to stdout. Died or killed?stderr
6Dec 12, 2023 08:4300000Error: No answer to stdout. Died or killed?stderr
7Dec 12, 2023 08:4300000Error: No answer to stdout. Died or killed?stderr
8Dec 12, 2023 08:4300000Error: No answer to stdout. Died or killed?stderr
9Dec 12, 2023 08:4300000Error: No answer to stdout. Died or killed?stderr
10Dec 12, 2023 08:4000000Error: No answer to stdout. Died or killed?stderr
11Dec 12, 2023 08:4400000Error: No answer to stdout. Died or killed?stderr
12Dec 12, 2023 08:4600000Error: No answer to stdout. Died or killed?stderr
13Dec 12, 2023 08:4600000Error: No answer to stdout. Died or killed?stderr
14Dec 12, 2023 08:4600000Error: No answer to stdout. Died or killed?stderr
15Dec 12, 2023 08:4600000Error: No answer to stdout. Died or killed?stderr
16Dec 12, 2023 08:4700000Error: No answer to stdout. Died or killed?stderr
17Dec 12, 2023 08:4700000Error: No answer to stdout. Died or killed?stderr
18Dec 12, 2023 08:3900000Error: No answer to stdout. Died or killed?stderr
19Dec 12, 2023 08:3900000Error: No answer to stdout. Died or killed?stderr
20Dec 12, 2023 08:3900000Error: No answer to stdout. Died or killed?stderr
21Dec 12, 2023 08:3800000Error: No answer to stdout. Died or killed?stderr
22Dec 12, 2023 08:3840,01268,584,82723,206,80902,232,320
23Dec 12, 2023 08:4040,10586,527,22923,260,89202,228,224
24Dec 12, 2023 08:4040,13192,253,34423,275,85502,252,800
25Dec 12, 2023 08:3840,18993,336,27423,309,74502,228,224
26Dec 12, 2023 08:3840,27591,570,26923,359,69402,220,032
27Dec 12, 2023 08:4240,28487,081,74523,364,73102,224,128
28Dec 12, 2023 08:4040,30784,236,96123,377,83402,232,320
29Dec 12, 2023 08:4240,32496,721,38823,388,15902,228,224
30Dec 12, 2023 08:4240,35890,076,75623,407,61002,232,320
31Dec 12, 2023 08:3840,41090,296,90423,437,69502,220,032
32Dec 12, 2023 08:3840,42488,238,09723,445,70302,236,416
33Dec 12, 2023 08:3840,50691,913,43823,493,61802,228,224