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 20, 2025 02:12HattonuriHattonuriScore: 36,864Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 20, 2025 02:2433,86645,580,48318,609,0001,033,0002,277,376
2Dec 20, 2025 02:2533,99045,766,95319,714,00002,527,232
3Dec 20, 2025 02:1534,44841,146,09518,981,000999,0002,277,376
4Dec 20, 2025 02:1235,06443,487,01320,337,00002,527,232
5Dec 20, 2025 02:1235,23343,917,71720,435,00002,277,376
6Dec 20, 2025 02:1235,26944,057,46120,456,00002,285,568
7Dec 20, 2025 02:2535,35244,851,87220,504,00002,531,328
8Dec 20, 2025 02:2536,02246,038,04419,899,000994,0002,527,232
9Dec 20, 2025 02:2536,02950,264,53819,902,000995,0002,277,376
10Dec 20, 2025 02:2436,04548,788,00320,906,00002,527,232
11Dec 20, 2025 02:2536,23848,316,15919,017,0002,001,0002,527,232
12Dec 20, 2025 02:1236,24547,959,21219,020,0002,002,0002,285,568
13Dec 20, 2025 02:2536,45046,035,21219,128,0002,013,0002,527,232
14Dec 20, 2025 02:2436,61043,604,69919,212,0002,022,0002,281,472
15Dec 20, 2025 02:1236,76044,177,34021,321,00002,277,376
16Dec 20, 2025 02:1336,78642,678,99720,320,0001,016,0002,269,184
17Dec 20, 2025 02:1236,86443,630,37321,381,00002,285,568
18Dec 20, 2025 02:1336,93838,375,30721,424,00002,375,680
19Dec 20, 2025 02:1237,42651,505,36521,707,00002,527,232
20Dec 20, 2025 02:2538,06444,214,03821,074,0001,003,0002,527,232
21Dec 20, 2025 02:1538,10944,842,94121,099,0001,004,0002,523,136
22Dec 20, 2025 02:2438,26647,146,60220,177,0002,017,0002,527,232
23Dec 20, 2025 02:1538,52443,861,75222,344,00002,281,472
24Dec 20, 2025 02:2438,53342,816,77622,349,00002,527,232
25Dec 20, 2025 02:1238,56251,439,33522,366,00002,281,472
26Dec 20, 2025 02:1238,82648,225,92121,496,0001,023,0002,527,232
27Dec 20, 2025 02:1238,99343,561,15421,588,0001,028,0002,527,232
28Dec 20, 2025 02:2439,17450,006,27022,721,00002,519,040
29Dec 20, 2025 02:1239,18147,390,72022,725,00002,519,040
30Dec 20, 2025 02:1339,22446,885,14122,750,00002,269,184
31Dec 20, 2025 02:2539,49044,483,02822,904,00002,523,136
32Dec 20, 2025 02:1240,23642,521,66823,337,00002,523,136
33Dec 20, 2025 02:2540,49544,271,11622,466,0001,021,0002,531,328