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 listMar 25, 2025 19:25Aniruddha DebAniruddha DebScore: 922,200Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 19, 2025 19:36772,771475,207,563445,206,0003,001,0002,420,736
2Apr 19, 2025 19:36777,166472,934,860446,759,0003,997,0002,420,736
3Apr 19, 2025 19:32781,338474,326,896453,176,00002,428,928
4Apr 19, 2025 19:45783,143485,463,066452,223,0002,000,0002,416,640
5Apr 19, 2025 19:36790,212486,883,547458,323,00002,433,024
6Apr 19, 2025 19:32791,076477,795,166454,826,0003,998,0002,400,256
7Apr 19, 2025 19:36791,684479,424,121458,177,0001,000,0002,424,832
8Apr 19, 2025 19:45807,395498,534,846467,289,0001,000,0002,506,752
9Apr 19, 2025 19:36813,183497,478,846468,649,0002,997,0002,408,448
10Apr 19, 2025 19:37817,493496,887,199472,146,0002,000,0002,392,064
11Apr 19, 2025 19:36817,828501,610,434471,338,0003,002,0002,416,640
12Apr 19, 2025 19:36820,048515,346,591473,630,0001,998,0002,383,872
13Apr 19, 2025 19:36823,919504,800,814474,874,0002,999,0002,387,968
14Apr 19, 2025 19:36826,924504,709,481478,617,000999,0002,379,776
15Apr 19, 2025 19:45827,353500,304,514476,866,0002,999,0002,383,872
16Apr 19, 2025 19:45832,966504,510,052481,120,0002,000,0002,363,392
17Apr 19, 2025 19:36837,064512,183,006483,500,0001,997,0002,367,488
18Apr 19, 2025 19:36849,678515,870,840490,814,0001,999,0002,281,472
19Apr 19, 2025 19:45854,759519,903,420493,761,0001,999,0002,355,200
20Apr 19, 2025 19:32875,590527,888,866505,843,0001,999,0002,359,296
21Apr 19, 2025 19:36878,324536,037,353505,433,0003,995,0002,392,064
22Apr 19, 2025 19:37890,322542,704,673515,387,0001,000,0002,457,600
23Apr 19, 2025 19:32895,562545,438,807518,426,0001,000,0002,396,160
24Apr 19, 2025 19:36897,172541,728,281517,364,0002,996,0002,392,064
25Apr 19, 2025 19:36901,053545,494,854520,613,0001,998,0002,367,488
26Apr 19, 2025 19:45904,845546,835,763521,812,0002,998,0002,387,968
27Apr 19, 2025 19:36911,712552,656,751525,795,0002,998,0002,281,472
28Apr 19, 2025 19:36922,200556,983,825533,877,000999,0002,383,872
29Apr 19, 2025 19:45934,138572,913,624538,802,0002,998,0002,392,064
30Apr 19, 2025 19:32943,574568,032,804545,272,0002,001,0002,363,392
31Apr 19, 2025 19:45946,507569,748,348543,975,0004,999,0002,457,600
32Apr 19, 2025 19:45948,905575,894,824548,364,0002,001,0002,371,584
33Apr 19, 2025 19:37961,452579,214,936553,645,0003,997,0002,281,472
34Apr 19, 2025 19:32964,745585,635,718558,553,000999,0002,351,104
35Apr 19, 2025 19:32966,747588,304,053559,714,000999,0002,392,064
36Apr 19, 2025 19:361,040,807622,482,879602,669,000999,0002,330,624
37Apr 19, 2025 19:321,043,086633,199,797602,991,0001,999,0002,404,352
38Apr 19, 2025 19:361,049,519633,820,454607,722,000999,0002,326,528
39Apr 19, 2025 19:321,076,566649,878,449621,411,0002,997,0002,338,816
40Mar 25, 2025 19:212,109,4571,248,533,0641,219,487,0003,998,0002,375,680
41Mar 25, 2025 19:212,118,8161,253,873,7151,225,914,0002,999,0002,453,504
42Mar 25, 2025 19:252,127,4241,255,930,2661,232,907,000999,0002,326,528
43Mar 25, 2025 19:252,129,5591,262,360,4931,234,145,000999,0002,359,296
44Mar 25, 2025 19:212,133,5071,259,689,6901,234,436,0002,998,0002,322,432
45Mar 25, 2025 19:212,137,6931,264,402,4981,236,863,0002,999,0002,310,144
46Mar 25, 2025 19:252,139,1711,261,192,0291,237,720,0002,999,0002,367,488
47Mar 25, 2025 19:252,146,0121,275,759,0011,241,688,0002,999,0002,359,296
48Mar 25, 2025 19:252,150,4471,267,820,0211,242,262,0004,997,0002,404,352
49Mar 25, 2025 19:252,150,4601,276,243,2111,243,270,0003,997,0002,506,752
50Mar 25, 2025 19:212,162,2381,278,547,0501,251,101,0002,997,0002,367,488
51Mar 25, 2025 19:252,170,5471,289,802,2971,256,918,0001,999,0002,375,680
52Mar 25, 2025 19:252,173,8971,287,709,7491,257,863,0002,997,0002,375,680
53Mar 25, 2025 19:212,175,2881,290,605,2391,259,668,0001,999,0002,375,680
54Mar 25, 2025 19:252,270,7811,339,062,9711,315,055,0001,998,0002,371,584