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 14, 2024 20:56__Error
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 15, 2024 09:5100000Error: Killed by timeout
2Feb 15, 2024 09:5400000Error: Killed by timeout
3Feb 15, 2024 09:5400000Error: Killed by timeout
4Feb 15, 2024 09:5300000Error: expected "1187290 ", got "1191741"
5Feb 15, 2024 09:5300000Error: Killed by timeout
6Feb 15, 2024 09:5300000Error: Killed by timeout
7Feb 15, 2024 09:5200000Error: Killed by timeout
8Feb 15, 2024 09:5100000Error: expected "774678 ", got "790429"
9Feb 15, 2024 09:5100000Error: Killed by timeout
10Feb 15, 2024 09:5100000Error: Killed by timeout
11Feb 15, 2024 09:5500000Error: expected "771812 ", got "774077"
12Feb 15, 2024 09:3500000Error: Killed by timeout
13Feb 15, 2024 09:3500000Error: expected "977011 ", got "978010"
14Feb 15, 2024 09:3500000Error: expected "1131700 ", got "1131393"
15Feb 15, 2024 09:3500000Error: Killed by timeout
16Feb 14, 2024 21:1300000Error: Killed by timeout
17Feb 14, 2024 21:1200000Error: Killed by timeout
18Feb 14, 2024 21:1200000Error: expected "1017589 ", got "1018099"
19Feb 14, 2024 21:1200000Error: expected "2794865 ", got "2806692"
20Feb 15, 2024 10:0300000Error: Killed by timeout
21Feb 15, 2024 10:0600000Error: Killed by timeout
22Feb 15, 2024 10:0600000Error: expected "912011 ", got "925789"
23Feb 15, 2024 10:0600000Error: expected "1148024 ", got "1146945"
24Feb 15, 2024 10:0500000Error: Killed by timeout
25Feb 15, 2024 10:0500000Error: Killed by timeout
26Feb 15, 2024 10:0400000Error: expected "1900792 ", got "1899061"
27Feb 15, 2024 10:0400000Error: Killed by timeout
28Feb 15, 2024 10:0400000Error: Killed by timeout
29Feb 14, 2024 21:1200000Error: Killed by timeout
30Feb 15, 2024 10:0300000Error: Killed by timeout
31Feb 15, 2024 10:0300000Error: Killed by timeout
32Feb 15, 2024 09:5800000Error: Killed by timeout
33Feb 15, 2024 09:5800000Error: Killed by timeout
34Feb 15, 2024 09:5800000Error: Killed by timeout
35Feb 15, 2024 09:5700000Error: Killed by timeout
36Feb 15, 2024 09:5700000Error: expected "1115811 ", got "1177687"
37Feb 15, 2024 09:5500000Error: Killed by timeout
38Feb 14, 2024 20:5700000Error: Killed by timeout
39Feb 14, 2024 21:0200000Error: expected "716651 ", got "717017"
40Feb 14, 2024 21:0200000Error: Killed by timeout
41Feb 14, 2024 21:0200000Error: Killed by timeout
42Feb 14, 2024 20:5800000Error: expected "2198858 ", got "2199356"
43Feb 14, 2024 20:5800000Error: Killed by timeout
44Feb 14, 2024 20:5700000Error: Killed by timeout
45Feb 14, 2024 20:5700000Error: Killed by timeout
46Feb 14, 2024 20:5700000Error: expected "774681 ", got "773589"
47Feb 14, 2024 21:0800000Error: expected "800695 ", got "805566"
48Feb 14, 2024 20:5700000Error: expected "3053953 ", got "3054200"
49Feb 14, 2024 20:5700000Error: Killed by timeout
50Feb 14, 2024 20:5700000Error: Killed by timeout
51Feb 14, 2024 20:5700000Error: expected "818004 ", got "817991"
52Feb 14, 2024 20:5700000Error: expected "1401863 ", got "1401535"
53Feb 14, 2024 20:5600000Error: Killed by timeout
54Feb 14, 2024 20:5600000Error: Killed by timeout
55Feb 14, 2024 21:0300000Error: expected "624319 ", got "630882"
56Feb 14, 2024 21:0800000Error: Killed by timeout
57Feb 14, 2024 21:0800000Error: Killed by timeout
58Feb 14, 2024 21:0800000Error: Killed by timeout
59Feb 14, 2024 21:0800000Error: expected "896763 ", got "903359"
60Feb 14, 2024 21:0700000Error: expected "1480978 ", got "1481009"
61Feb 14, 2024 21:0700000Error: Killed by timeout
62Feb 14, 2024 21:0700000Error: Killed by timeout
63Feb 14, 2024 21:0500000Error: expected "2002298 ", got "2002274"
64Feb 14, 2024 21:0500000Error: Killed by timeout
65Feb 14, 2024 21:0400000Error: Killed by timeout
66Feb 14, 2024 21:0400000Error: Killed by timeout
67Feb 14, 2024 21:0300000Error: expected "687681 ", got "686917"
68Feb 14, 2024 21:0300000Error: Killed by timeout
69Feb 14, 2024 21:0300000Error: expected "1711954 ", got "1714948"
70Feb 14, 2024 21:0200000Error: Killed by timeout
71Feb 15, 2024 10:0456,61893,820,15732,838,46602,199,552
72Feb 15, 2024 10:0461,247106,421,54535,523,35902,191,360
73Feb 15, 2024 10:0462,276107,339,75236,119,86602,195,456