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 21:35@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:34@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:31@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:30@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:26@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:25@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:24@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 21:14@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:40@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:38@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:37@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:26@drytecc@dryteccCPPclang++18.1.3Success71,891
Oct 15, 2024 20:24@drytecc@dryteccCPPclang++18.1.3Success68,005
Oct 15, 2024 20:19@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:09@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:09@drytecc@dryteccCPPclang++18.1.3Success67,278+148.39 RP
Oct 15, 2024 20:08@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:07@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:06@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 20:00@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:56@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:54@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:52@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:50@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:50@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:49@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:47@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:46@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:44@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:43@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:42@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:39@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:37@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:32@drytecc@dryteccCPPclang++18.1.3Success40,213,452+0.25 RP
Oct 15, 2024 19:30@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:29@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 19:29@drytecc@dryteccCPPclang++18.1.3Error
Oct 15, 2024 18:43vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:42vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:41vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:40vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:39vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:36vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:36vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:35vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:33vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:32vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:31vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:30vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:29vctorizedvctorizedRUSTrust-1.76.0Error
Oct 15, 2024 18:28vctorizedvctorizedRUSTrust-1.76.0Error
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