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
Oct 15, 2024 16:29@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:28@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:28@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:26@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:26@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:24@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:23@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 16:23@drytecc@dryteccCPPclang++18.1.3Error
Oct 13, 2024 22:05korey-skorey-sRUSTrust-1.81.0Success136,943
Oct 4, 2024 08:11vctorizedvctorizedRUSTrust-1.76.0Error
Sep 12, 2024 12:22matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 12, 2024 12:22matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 12, 2024 12:20matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 12, 2024 12:19matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 10, 2024 17:00matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 10, 2024 16:58matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 10, 2024 16:57matsuoka-601matsuoka-601CPPclang++18.1.3Error
Sep 4, 2024 13:34Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:33Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:27Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:25Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:22Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:19Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Sep 4, 2024 13:17Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Sep 4, 2024 13:16Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Success39,014
Sep 3, 2024 16:46AnSaAnSaCPPclang++18.1.3Success2,371,024
Sep 3, 2024 16:45AnSaAnSaCPPg++13.2.0Success2,437,757
Aug 31, 2024 22:08Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Aug 31, 2024 22:08Yuriy LyfenkoYuriy LyfenkoCPPclang++18.1.3Error
Aug 31, 2024 22:07Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Aug 31, 2024 22:06Yuriy LyfenkoYuriy LyfenkoCPPg++13.2.0Error
Aug 3, 2024 00:12Henry CafaroHenry CafaroCPPg++9.4.0Success2,243,969+4.46 RP
Jul 24, 2024 22:070xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 22:050xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 22:040xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 22:030xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 22:020xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 22:020xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 21:580xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 21:570xkake0xkakeRUSTrust-1.79.0Error
Jul 24, 2024 20:330xkake0xkakeRUSTrust-1.79.0Success112,553
Jul 3, 2024 17:15E SequeiraE SequeiraCPPclang++10.0.0Error
Jul 3, 2024 15:44E SequeiraE SequeiraCPPclang++10.0.0Error
Jul 3, 2024 15:38E SequeiraE SequeiraCPPclang++10.0.0Error
Jul 3, 2024 15:13E SequeiraE SequeiraCPPclang++10.0.0Success77,847
Jul 3, 2024 11:35E SequeiraE SequeiraGOgo1.22.5Success1,368,127
Jun 24, 2024 16:34RahulRahulCPPclang++10.0.0Error
Jun 24, 2024 16:01RahulRahulCPPclang++10.0.0Error
Jun 24, 2024 15:28RahulRahulCPPclang++10.0.0Success140,423+2.51 RP
Jun 24, 2024 03:56RahulRahulCPPg++9.4.0Success145,559+28.47 RP
Jun 24, 2024 03:55RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:21RahulRahulCPPclang++10.0.0Success342,396
Jun 20, 2024 23:21RahulRahulCPPg++9.4.0Success1,028,495
Jun 20, 2024 23:19RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:18RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:17RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:16RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:16RahulRahulCPPg++9.4.0Error
Jun 20, 2024 23:12RahulRahulCPPg++9.4.0Error
Jun 20, 2024 22:56RahulRahulCPPg++9.4.0Error
Jun 20, 2024 22:55RahulRahulCPPg++9.4.0Error
Jun 20, 2024 22:54RahulRahulCPPg++9.4.0Error
Jun 20, 2024 22:54RahulRahulCPPg++9.4.0Error
Jun 20, 2024 21:55RahulRahulCPPg++9.4.0Success329,106
Jun 20, 2024 21:55RahulRahulCPPg++9.4.0Success326,503
Jun 20, 2024 21:53RahulRahulCPPg++9.4.0Success333,127
Jun 20, 2024 21:21RahulRahulCPPg++9.4.0Success321,748
Jun 20, 2024 21:20RahulRahulCPPclang++10.0.0Error
Jun 20, 2024 20:26RahulRahulCPPg++9.4.0Success317,054
Jun 20, 2024 19:42RahulRahulCPPg++9.4.0Success315,814
Jun 20, 2024 19:41RahulRahulCPPg++9.4.0Error
Jun 20, 2024 18:49RahulRahulCPPg++9.4.0Success295,418
Jun 20, 2024 18:28RahulRahulCPPg++9.4.0Success286,274
Jun 20, 2024 18:28RahulRahulCPPg++9.4.0Success248,555+4.88 RP
Jun 20, 2024 18:27RahulRahulCPPclang++10.0.0Success282,851+0.10 RP
Jun 20, 2024 18:06RahulRahulCPPg++9.4.0Success293,059
Jun 20, 2024 18:05RahulRahulCPPg++9.4.0Error
Jun 20, 2024 17:51RahulRahulCPPg++9.4.0Success283,681+29.43 RP
Jun 18, 2024 19:12Andrey SolovyevAndrey SolovyevCPPg++9.4.0Success42,289,504
Jun 18, 2024 18:49Andrey SolovyevAndrey SolovyevCPPg++9.4.0Error
Jun 18, 2024 18:43Andrey SolovyevAndrey SolovyevCPPg++9.4.0Error
Jun 18, 2024 17:57RahulRahulCPPg++9.4.0Error
Jun 18, 2024 17:55RahulRahulCPPg++9.4.0Error
Jun 18, 2024 17:48RahulRahulCPPg++9.4.0Error
Jun 18, 2024 17:46RahulRahulCPPclang++10.0.0Error
Jun 18, 2024 17:45RahulRahulCPPclang++10.0.0Error
Jun 18, 2024 17:18Andrey SolovyevAndrey SolovyevCPPg++9.4.0Success2,244,356+4.46 RP
Jun 18, 2024 16:54RahulRahulCPPclang++10.0.0Success1,718,849+0.01 RP
Jun 18, 2024 16:53RahulRahulCPPg++9.4.0Success1,722,466+1.37 RP
Jun 18, 2024 16:41RahulRahulCPPg++9.4.0Error
Jun 18, 2024 16:17RahulRahulCPPg++9.4.0Success2,252,565+4.44 RP
Jun 11, 2024 15:56matsuoka-601matsuoka-601CPPclang++10.0.0Error
Jun 11, 2024 15:53matsuoka-601matsuoka-601CPPclang++10.0.0Error
Jun 11, 2024 15:49matsuoka-601matsuoka-601CPPclang++10.0.0Error
May 25, 2024 06:01Tejas G.Tejas G.CPPg++9.4.0Error
May 25, 2024 05:59Tejas G.Tejas G.CPPg++9.4.0Success48,806
May 16, 2024 19:39nnGOgo1.22.3Success1,369,477+2.85 RP
May 16, 2024 19:39nnCPPg++9.4.0Success2,245,894+4.45 RP
May 11, 2024 17:35stdspstdspRUSTrust-1.78.0Success214,086
May 11, 2024 16:48stdspstdspRUSTrust-1.78.0Success110,359