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.

Date AuthorLanguageStatus Score
Nov 4, 2026 13:04Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success31,217
Nov 4, 2026 13:00Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success496,436
Nov 4, 2026 12:34Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success534,010
Nov 4, 2026 11:35Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success279,762
Nov 4, 2026 11:08Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success396,286
Nov 4, 2026 10:55Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success714,037
Nov 4, 2026 10:26Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success31,496
Nov 4, 2026 10:21Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success459,627
Nov 4, 2026 10:04Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success315,965
Nov 4, 2026 09:57Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success379,679
Nov 4, 2026 09:30Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success32,015
Nov 4, 2026 09:24Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success180,162
Nov 4, 2026 09:03Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success238,513
Nov 4, 2026 08:12Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success348,891
Nov 4, 2026 07:35Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success514,932
Nov 4, 2026 07:15Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success200,900
Nov 4, 2026 07:08Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success429,982
Nov 4, 2026 06:47Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success282,896
Nov 4, 2026 06:39Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success186,687
Nov 4, 2026 06:30Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success238,044
Nov 4, 2026 05:55Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success601,770
Nov 4, 2026 05:26Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success185,386
Nov 4, 2026 05:08Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success259,172
Nov 4, 2026 04:54Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success243,046
Nov 4, 2026 04:54Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success254,813
Nov 4, 2026 04:26Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success267,146
Nov 4, 2026 04:19Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success275,565
Nov 4, 2026 04:12Andrew EpsteinAndrew EpsteinCPPg++14.2.0Error
Nov 4, 2026 04:12Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success4,396
Nov 4, 2026 04:10Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success4,589
Nov 4, 2026 04:03Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success4,434
Nov 4, 2026 03:58Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success67,772
Nov 4, 2026 03:56Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success5,103
Nov 4, 2026 03:18Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success21,274
Nov 4, 2026 03:12Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success486,077
Nov 4, 2026 03:00Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success360,624
Nov 4, 2026 02:40Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success264,627
Nov 4, 2026 02:35Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success32,968
Nov 4, 2026 02:33Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success68,758
Nov 4, 2026 02:19Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success34,162
Nov 4, 2026 02:16Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success373,763
Nov 4, 2026 02:10Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success452,465
Nov 4, 2026 02:00Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success418,424
Nov 4, 2026 01:41Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success413,725
Nov 4, 2026 01:38Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success39,246
Nov 4, 2026 01:35Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success384,962
Nov 4, 2026 01:16Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success390,739
Nov 4, 2026 01:02Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success34,803
Nov 4, 2026 01:00Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success401,506
Nov 4, 2026 00:52Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success548,141
Nov 4, 2026 00:32Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success540,175
Nov 4, 2026 00:17Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success36,141
Nov 4, 2026 00:10Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success558,112
Nov 4, 2026 00:05Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success38,655
Nov 4, 2026 00:01Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success584,132
Nov 3, 2026 23:43Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success39,715
Nov 3, 2026 23:22Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success527,989
Oct 21, 2026 03:54highload-churnerhighload-churnerCPPg++14.2.0Success2,491
Oct 21, 2026 03:32highload-churnerhighload-churnerCPPg++14.2.0Success2,420
Oct 21, 2026 03:25highload-churnerhighload-churnerCPPg++14.2.0Success2,636
Oct 21, 2026 03:24highload-churnerhighload-churnerCPPg++14.2.0Success2,420+897.98 RP
Oct 21, 2026 03:22highload-churnerhighload-churnerCPPg++14.2.0Success44,227
Oct 21, 2026 03:15highload-churnerhighload-churnerCPPg++14.2.0Success2,977
Oct 21, 2026 02:34highload-churnerhighload-churnerCPPg++14.2.0Success2,715+3'642.25 RP
Oct 21, 2026 02:26highload-churnerhighload-churnerCPPg++14.2.0Success68,832
Oct 21, 2026 02:21highload-churnerhighload-churnerCPPg++14.2.0Success18,791
Oct 21, 2026 01:41highload-churnerhighload-churnerCPPg++14.2.0Success223,808
Oct 21, 2026 01:37highload-churnerhighload-churnerCPPg++14.2.0Success432,546
Oct 21, 2026 01:36highload-churnerhighload-churnerCPPg++14.2.0Success364,213
Oct 21, 2026 01:28highload-churnerhighload-churnerCPPg++14.2.0Success2,615,770
Oct 20, 2026 21:03janwwwjanwwwRUSTrust-1.95.0Success35,425
Oct 20, 2026 20:37janwwwjanwwwCPPclang++20.1.2Success32,856+14.75 RP
Oct 20, 2026 11:46C21C21CPPclang++20.1.2Success141,970+52.28 RP
Oct 20, 2026 03:27highload-churnerhighload-churnerCPPg++14.2.0Success16,474+37.38 RP
Oct 20, 2026 00:45highload-churnerhighload-churnerCPPg++14.2.0Success17,655
Oct 20, 2026 00:36highload-churnerhighload-churnerCPPg++14.2.0Success17,713
Oct 20, 2026 00:34highload-churnerhighload-churnerCPPg++14.2.0Success17,574
Oct 20, 2026 00:25highload-churnerhighload-churnerCPPg++14.2.0Success17,555+15.81 RP
Oct 19, 2026 15:38highload-churnerhighload-churnerCPPg++14.2.0Success18,056+2.69 RP
Oct 19, 2026 15:31highload-churnerhighload-churnerCPPg++14.2.0Success18,144+12.79 RP
Oct 19, 2026 15:23highload-churnerhighload-churnerCPPg++14.2.0Success18,575+24.43 RP
Oct 19, 2026 15:20C21C21CPPclang++20.1.2Success550,836+6.45 RP
Oct 19, 2026 15:12highload-churnerhighload-churnerCPPg++14.2.0Success19,458+3.31 RP
Oct 19, 2026 14:57highload-churnerhighload-churnerCPPg++14.2.0Success19,584+40.52 RP
Oct 19, 2026 14:52C21C21CPPclang++20.1.2Error
Oct 19, 2026 14:42C21C21CPPclang++20.1.2Error
Oct 19, 2026 14:40C21C21CPPclang++20.1.2Success854,048+11.71 RP
Oct 19, 2026 12:38highload-churnerhighload-churnerCPPg++14.2.0Success36,627
Oct 19, 2026 12:21highload-churnerhighload-churnerCPPg++14.2.0Success28,463
Oct 19, 2026 11:50highload-churnerhighload-churnerCPPg++14.2.0Success30,820
Oct 19, 2026 11:20highload-churnerhighload-churnerCPPg++14.2.0Success28,717
Oct 19, 2026 10:58highload-churnerhighload-churnerCPPg++14.2.0Success37,346
Oct 19, 2026 10:53highload-churnerhighload-churnerCPPg++14.2.0Success22,075
Oct 19, 2026 09:04highload-churnerhighload-churnerCPPg++14.2.0Success21,272+78.91 RP
Oct 19, 2026 06:58highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 04:10zayd-ansari-codezayd-ansari-codeCPPg++10.5.0Success1,307,479+7.65 RP
Oct 18, 2026 09:01KacpxerKacpxerCPPg++14.2.0Error+31.20 RP
Oct 17, 2026 09:59KacpxerKacpxerCPPclang++20.1.2Error+202.65 RP
Oct 17, 2026 07:42janwwwjanwwwRUSTrust-1.95.0Error+192.48 RP
Oct 16, 2026 22:41Karthik ReddyKarthik ReddyCPPg++14.2.0Success5,800