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 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
Oct 16, 2026 22:39Karthik ReddyKarthik ReddyCPPg++14.2.0Success5,618
Oct 14, 2026 00:10highload-churnerhighload-churnerCPPg++14.2.0Success25,563+65.39 RP
Oct 13, 2026 21:21janwwwjanwwwCPPclang++20.1.2Error+209.67 RP
Oct 13, 2026 15:14highload-churnerhighload-churnerCPPg++14.2.0Success30,694+79.26 RP
Oct 13, 2026 15:10highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 13, 2026 15:09highload-churnerhighload-churnerCPPclang++18.1.3Success123,662
Oct 13, 2026 14:47highload-churnerhighload-churnerCPPg++14.2.0Success40,562+60.73 RP
Oct 13, 2026 13:57highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 13, 2026 13:56highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 13, 2026 13:40highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 13, 2026 13:36highload-churnerhighload-churnerCPPg++14.2.0Success59,393
Oct 11, 2026 08:56janwwwjanwwwCPPclang++20.1.2Error+172.39 RP
Oct 10, 2026 23:14janwwwjanwwwCPPg++14.2.0Success102,953
Oct 10, 2026 00:18highload-churnerhighload-churnerCPPg++14.2.0Success60,155
Oct 9, 2026 23:23highload-churnerhighload-churnerCPPg++14.2.0Success104,050
Oct 9, 2026 23:03highload-churnerhighload-churnerCPPg++14.2.0Success53,820+9.49 RP
Oct 9, 2026 22:55highload-churnerhighload-churnerCPPg++14.2.0Success56,717+3.08 RP
Oct 9, 2026 22:15highload-churnerhighload-churnerCPPg++14.2.0Success99,698
Oct 9, 2026 21:28highload-churnerhighload-churnerCPPg++14.2.0Success103,789
Oct 9, 2026 20:30highload-churnerhighload-churnerCPPg++14.2.0Success57,725+1.94 RP
Oct 9, 2026 20:17highload-churnerhighload-churnerCPPg++14.2.0Success58,837
Oct 9, 2026 20:02highload-churnerhighload-churnerCPPg++14.2.0Success58,379+5.26 RP
Oct 9, 2026 19:54highload-churnerhighload-churnerCPPg++14.2.0Error+67.14 RP
Oct 9, 2026 19:24highload-churnerhighload-churnerCPPg++14.2.0Success102,229
Oct 9, 2026 19:19highload-churnerhighload-churnerCPPg++14.2.0Success101,112+2.97 RP
Oct 9, 2026 17:35Karthik ReddyKarthik ReddyCSHARP9.0.18Success6,541
Oct 9, 2026 16:07Karthik ReddyKarthik ReddyCSHARP9.0.18Success6,474
Oct 9, 2026 15:53Karthik ReddyKarthik ReddyCSHARP9.0.18Success6,282
Oct 9, 2026 15:52Karthik ReddyKarthik ReddyCSHARP9.0.18Success6,431
Oct 9, 2026 15:48highload-churnerhighload-churnerCPPg++14.2.0Error+95.94 RP
Oct 9, 2026 15:38Karthik ReddyKarthik ReddyCSHARP9.0.18Success6,565
Oct 9, 2026 15:03Karthik ReddyKarthik ReddyCSHARP9.0.18Success3,701
Oct 9, 2026 14:35Karthik ReddyKarthik ReddyCSHARP9.0.18Success8,075
Oct 9, 2026 12:16janwwwjanwwwCPPg++14.2.0Error+129.96 RP
Oct 9, 2026 11:39Karthik ReddyKarthik ReddyGOgo1.26.2Success3,220+227.06 RP
Oct 9, 2026 11:39Karthik ReddyKarthik ReddyRUSTrust-1.95.0Success3,046+395.18 RP
Oct 9, 2026 10:44Karthik ReddyKarthik ReddyGOgo1.26.2Success5,560
Oct 9, 2026 10:43Karthik ReddyKarthik ReddyZIG0.15.1Success3,132+340.23 RP
Oct 9, 2026 10:43Karthik ReddyKarthik ReddyRUSTrust-1.95.0Success5,477
Oct 9, 2026 09:00Karthik ReddyKarthik ReddyZIG0.15.1Success5,606
Oct 9, 2026 08:59Karthik ReddyKarthik ReddyRUSTrust-1.95.0Success5,534