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 11, 2022 19:09Istomin MikhailIstomin MikhailScore: 152,478Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Feb 11, 2022 19:08125,624104,283,63769,868,0002,994,0002,293,760
2Feb 11, 2022 19:08128,42497,863,62073,480,0001,006,0002,506,752
3Feb 11, 2022 19:09128,598100,899,89172,572,0002,015,0002,326,528
4Feb 11, 2022 19:09130,753102,534,17274,840,000997,0002,510,848
5Feb 11, 2022 19:09135,707105,909,08776,692,0002,018,0002,297,856
6Feb 11, 2022 19:08142,471110,863,65782,633,00002,293,760
7Feb 11, 2022 19:08152,478140,474,51088,437,32102,195,456
8Feb 11, 2022 19:09153,237145,003,07388,877,57202,203,648
9Feb 11, 2022 19:08153,959144,876,93189,296,27102,191,360
10Feb 11, 2022 19:09154,002153,834,80589,321,22902,191,360
11Feb 11, 2022 19:09155,568151,298,46090,229,30202,183,168
12Feb 11, 2022 19:08157,862157,424,67291,559,73402,187,264