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 24, 2024 18:50Joad 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 24, 2024 18:5500000Error: expected "1756163 ", got "1757122 "stderr
2Nov 24, 2024 18:5300000Error: expected "1026985 ", got "1029820 "stderr
3Nov 24, 2024 18:5300000Error: expected "1282947 ", got "1284720 "stderr
4Nov 24, 2024 18:5300000Error: expected "2370673 ", got "2329908 "stderr
5Nov 24, 2024 18:5300000Error: expected "2690561 ", got "2309632 "stderr
6Nov 24, 2024 18:5300000Error: expected "1527746 ", got "1310000 "stderr
7Nov 24, 2024 18:5300000Error: expected "2175697 ", got "2175985 "stderr
8Nov 24, 2024 18:5400000Error: expected "1211288 ", got "1211694 "stderr
9Nov 24, 2024 18:5400000Error: expected "2690604 ", got "2594938 "stderr
10Nov 24, 2024 18:5500000Error: expected "1547726 ", got "1506000 "stderr
11Nov 24, 2024 18:5300000Error: expected "1148773 ", got "1148542 "stderr
12Nov 24, 2024 18:5500000Error: expected "1545147 ", got "1495332 "stderr
13Nov 24, 2024 18:5500000Error: expected "2085378 ", got "2086083 "stderr
14Nov 24, 2024 18:5500000Error: expected "1281614 ", got "1278870 "stderr
15Nov 24, 2024 18:5500000Error: expected "885783 ", got "886007 "stderr
16Nov 24, 2024 18:5500000Error: expected "1905141 ", got "1736248 "stderr
17Nov 24, 2024 18:5600000Error: expected "1867227 ", got "1718000 "stderr
18Nov 24, 2024 18:5600000Error: expected "2200701 ", got "2198568 "stderr
19Nov 24, 2024 18:5600000Error: expected "2598626 ", got "2598384 "stderr
20Nov 24, 2024 18:5200000Error: expected "2154793 ", got "1850000 "stderr
21Nov 24, 2024 18:5100000Error: expected "1596510 ", got "1515043 "stderr
22Nov 24, 2024 18:5100000Error: expected "3560973 ", got "3492991 "stderr
23Nov 24, 2024 18:5100000Error: expected "652727 ", got "652211 "stderr
24Nov 24, 2024 18:5100000Error: expected "1726443 ", got "1627000 "stderr
25Nov 24, 2024 18:5100000Error: expected "2365219 ", got "2365438 "stderr
26Nov 24, 2024 18:5200000Error: expected "950774 ", got "950522 "stderr
27Nov 24, 2024 18:5200000Error: expected "1054635 ", got "1054550 "stderr
28Nov 24, 2024 18:5200000Error: expected "1840206 ", got "1838781 "stderr
29Nov 24, 2024 18:5000000Error: expected "1933811 ", got "1892904 "stderr
30Nov 24, 2024 18:5200000Error: expected "2303772 ", got "2280299 "stderr
31Nov 24, 2024 18:5200000Error: expected "2498641 ", got "2499485 "stderr
32Nov 24, 2024 18:5200000Error: expected "1731033 ", got "1732114 "stderr
33Nov 24, 2024 18:5200000Error: expected "2055835 ", got "2056910 "stderr
34Nov 24, 2024 18:5200000Error: expected "2781350 ", got "2633533 "stderr
35Nov 24, 2024 18:5200000Error: expected "1763141 ", got "1493000 "stderr
36Nov 24, 2024 18:5200000Error: expected "658342 ", got "658252 "stderr
37Nov 24, 2024 18:5200000Error: expected "812214 ", got "812247 "stderr