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 listNov 27, 2024 19:57Joad NacerJoad NacerError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Nov 27, 2024 19:5900000Error: expected "2888241 ", got "2447000 "stderr
2Nov 27, 2024 19:5900000Error: expected "2951313 ", got "2740000 "stderr
3Nov 27, 2024 19:5900000Error: expected "629963 ", got "617172 "stderr
4Nov 27, 2024 19:5900000Error: expected "1419935 ", got "1417597 "stderr
5Nov 27, 2024 19:5900000Error: expected "691343 ", got "689735 "stderr
6Nov 27, 2024 19:5900000Error: expected "922644 ", got "830000 "stderr
7Nov 27, 2024 19:5900000Error: expected "1763020 ", got "1752385 "stderr
8Nov 27, 2024 19:5900000Error: No answer to stdout. Died or killed?stderr
9Nov 27, 2024 19:5900000Error: expected "2152393 ", got "1895000 "stderr
10Nov 27, 2024 19:5900000Error: expected "827899 ", got "787794 "stderr
11Nov 27, 2024 19:5900000Error: expected "2150525 ", got "2146652 "stderr
12Nov 27, 2024 19:5900000Error: No answer to stdout. Died or killed?stderr
13Nov 27, 2024 19:5900000Error: expected "2676744 ", got "2668068 "stderr
14Nov 27, 2024 19:5900000Error: expected "625153 ", got "608765 "stderr
15Nov 27, 2024 19:5900000Error: expected "1384517 ", got "1327000 "stderr
16Nov 27, 2024 19:5900000Error: expected "1606243 ", got "1509262 "stderr
17Nov 27, 2024 19:5700000Error: expected "1713915 ", got "1707664 "stderr
18Nov 27, 2024 19:5700000Error: No answer to stdout. Died or killed?stderr
19Nov 27, 2024 19:5700000Error: expected "1992761 ", got "1823894 "stderr
20Nov 27, 2024 19:5700000Error: expected "2806286 ", got "2484000 "stderr
21Nov 27, 2024 19:5700000Error: expected "1426422 ", got "1425434 "stderr
22Nov 27, 2024 19:5700000Error: expected "1461840 ", got "1413000 "stderr
23Nov 27, 2024 19:5700000Error: expected "920327 ", got "819544 "stderr
24Nov 27, 2024 19:5700000Error: expected "1726361 ", got "1539000 "stderr
25Nov 27, 2024 19:5700000Error: expected "1187464 ", got "1152954 "stderr
26Nov 27, 2024 19:5700000Error: expected "1106433 ", got "1106163 "stderr
27Nov 27, 2024 19:5700000Error: expected "1114414 ", got "936000 "stderr
28Nov 27, 2024 19:5700000Error: expected "1397667 ", got "1264000 "stderr
29Nov 27, 2024 19:5700000Error: expected "2052130 ", got "2015872 "stderr
30Nov 27, 2024 19:5700000Error: expected "1002052 ", got "999975 "stderr
31Nov 27, 2024 19:5700000Error: expected "1907562 ", got "1870000 "stderr
32Nov 27, 2024 19:5700000Error: expected "2475933 ", got "2388515 "stderr