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 listDec 27, 2024 11:13Louis PonetLouis PonetScore: 281,690Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 27, 2024 11:13280,722191,560,225148,835,00013,984,0002,465,792
2Dec 27, 2024 11:47280,981189,838,834149,972,00012,997,0002,387,968
3Dec 27, 2024 11:13281,047195,921,062148,007,00015,000,0002,359,296
4Dec 27, 2024 11:13281,179192,423,569150,078,00013,006,0002,306,048
5Dec 27, 2024 11:13281,210189,446,291150,094,00013,008,0002,297,856
6Dec 27, 2024 11:47281,210190,956,650149,094,00014,008,0002,428,928
7Dec 27, 2024 11:47281,484189,010,568150,241,00013,020,0002,301,952
8Dec 27, 2024 11:47281,486189,888,124152,245,00011,017,0002,359,296
9Dec 27, 2024 11:47281,516187,529,351151,259,00012,020,0002,396,160
10Dec 27, 2024 11:13281,690197,432,701149,348,00014,032,0002,465,792
11Dec 27, 2024 11:13281,838193,351,962150,429,00013,037,0002,441,216
12Dec 27, 2024 11:13282,041191,586,927153,549,00010,035,0002,502,656
13Dec 27, 2024 11:13282,134189,201,365153,599,00010,039,0002,342,912
14Dec 27, 2024 11:47282,148191,774,358151,599,00012,047,0002,297,856
15Dec 27, 2024 11:13282,155191,548,993151,603,00012,047,0002,416,640
16Dec 27, 2024 11:47282,402189,493,122149,811,00013,982,0002,453,504
17Dec 27, 2024 11:47283,407192,439,617149,342,00015,034,0002,428,928
18Dec 27, 2024 11:47284,321195,375,285153,913,00010,993,0002,277,376