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 listApr 1, 2025 15:15Yuriy LyfenkoYuriy LyfenkoScore: 47,803Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 1, 2025 15:1741,72945,635,01623,195,0001,008,0002,359,296
2Apr 1, 2025 15:1743,19054,431,86122,044,0003,006,0002,347,008
3Apr 1, 2025 15:1743,43148,499,91924,183,0001,007,0002,486,272
4Apr 1, 2025 15:1643,50545,228,71824,224,0001,009,0002,338,816
5Apr 1, 2025 15:1643,56749,247,43525,269,00002,265,088
6Apr 1, 2025 15:1744,70044,949,17224,929,000997,0002,473,984
7Apr 1, 2025 15:1744,71757,484,86223,941,0001,995,0002,293,760
8Apr 1, 2025 15:1645,06753,915,81325,134,0001,005,0002,498,560
9Apr 1, 2025 15:1745,68353,262,32925,515,000981,0002,330,624
10Apr 1, 2025 15:1646,00052,949,20926,680,00002,449,408
11Apr 1, 2025 15:1646,48456,059,27624,964,0001,997,0002,531,328
12Apr 1, 2025 15:1647,15948,540,79427,352,00002,461,696
13Apr 1, 2025 15:1647,80352,199,51427,726,00002,285,568
14Apr 1, 2025 15:1648,14752,809,58626,928,000997,0002,523,136
15Apr 1, 2025 15:1648,16752,203,98626,940,000997,0002,416,640
16Apr 1, 2025 15:1749,81453,917,49426,900,0001,992,0002,461,696
17Apr 1, 2025 15:1749,85351,834,92326,921,0001,994,0002,457,600
18Apr 1, 2025 15:1651,58455,775,35827,925,0001,994,0002,289,664
19Apr 1, 2025 15:1754,40260,005,02831,553,00002,347,008
20Apr 1, 2025 15:1655,53454,434,28230,197,0002,013,0002,387,968
21Apr 1, 2025 15:1656,09853,141,31632,537,00002,473,984
22Apr 1, 2025 15:1595,63375,039,22254,459,0001,008,0002,469,888
23Apr 1, 2025 15:15100,08682,409,60057,050,0001,000,0002,285,568
24Apr 1, 2025 15:15101,78386,023,52657,033,0002,001,0002,473,984