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
Dec 5, 2023 09:07matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 09:03matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 09:01matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 09:00__RUSTrust-1.74.0Error
Dec 5, 2023 09:00__RUSTrust-1.74.0Success69,805
Dec 5, 2023 08:59__RUSTrust-1.74.0Error
Dec 5, 2023 08:58__RUSTrust-1.74.0Error
Dec 5, 2023 08:55Sergey StreminSergey StreminRUSTrust-1.59.0Success86,000
Dec 5, 2023 08:55Sergey StreminSergey StreminRUSTrust-1.59.0Success84,700
Dec 5, 2023 08:54matsuoka-601matsuoka-601CPPclang++10.0.0Success99,126
Dec 5, 2023 08:54Sergey StreminSergey StreminRUSTrust-1.59.0Success82,091
Dec 5, 2023 08:49__RUSTrust-1.74.0Success71,166
Dec 5, 2023 08:47__RUSTrust-1.74.0Error
Dec 5, 2023 08:47matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:45__RUSTrust-1.74.0Error
Dec 5, 2023 08:44matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:44__RUSTrust-1.74.0Error
Dec 5, 2023 08:43__RUSTrust-1.74.0Error
Dec 5, 2023 08:42matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:41__RUSTrust-1.74.0Success68,972+35.85 RP
Dec 5, 2023 08:41matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:40__RUSTrust-1.74.0Error
Dec 5, 2023 08:38matsuoka-601matsuoka-601CPPclang++10.0.0Success99,363
Dec 5, 2023 08:38matsuoka-601matsuoka-601CPPg++9.3.0Success96,604
Dec 5, 2023 08:37matsuoka-601matsuoka-601CPPg++9.3.0Error
Dec 5, 2023 08:37__RUSTrust-1.74.0Error
Dec 5, 2023 08:36__RUSTrust-1.74.0Error
Dec 5, 2023 08:35matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:35matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:35matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:34__RUSTrust-1.74.0Error
Dec 5, 2023 08:33matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:33matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:33matsuoka-601matsuoka-601CPPclang++10.0.0Error
Dec 5, 2023 08:18__RUSTrust-1.74.0Error
Dec 5, 2023 08:14__RUSTrust-1.74.0Success82,981+1.91 RP
Dec 5, 2023 08:07__RUSTrust-1.74.0Error
Dec 5, 2023 07:51Sergey StreminSergey StreminRUSTrust-1.59.0Success74,843+1.92 RP
Dec 5, 2023 07:43Sergey StreminSergey StreminRUSTrust-1.59.0Success75,384+8.40 RP
Dec 5, 2023 07:41Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 5, 2023 07:38Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 5, 2023 06:58Sergey StreminSergey StreminRUSTrust-1.59.0Success77,850+22.07 RP
Dec 5, 2023 06:55Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 5, 2023 06:52Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 5, 2023 04:50matsuoka-601matsuoka-601CPPg++9.3.0Error
Dec 5, 2023 04:49matsuoka-601matsuoka-601CPPg++9.3.0Error
Dec 4, 2023 22:36parastromparastromRUSTrust-1.74.0Success961,341+7.79 RP
Dec 4, 2023 22:35parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 22:33parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 22:14parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 22:04parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 22:00parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 21:04parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 16:22Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 16:20Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 16:18Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 16:16Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:59Sergey StreminSergey StreminRUSTrust-1.59.0Success86,936
Dec 4, 2023 15:58Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 15:40parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 15:22Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Success56,239+17.53 RP
Dec 4, 2023 15:14Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:13Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:12Sergey StreminSergey StreminRUSTrust-1.59.0Success93,821
Dec 4, 2023 15:11Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:07Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:03Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:01Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 15:00Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 14:58Sergey StreminSergey StreminRUSTrust-1.59.0Success5,654,682
Dec 4, 2023 14:57Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Error
Dec 4, 2023 14:55Yuriy LyfenkoYuriy LyfenkoCSHARP8.0.0Success63,584
Dec 4, 2023 14:16parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 14:13parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 14:10nolipolsupnolipolsupCPPg++9.3.0Success2,261,770+4.42 RP
Dec 4, 2023 14:09parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:52parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:51parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:49parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:48parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:46parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:33parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:32parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:29parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:25parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 13:07Will RiegerWill RiegerRUSTrust-1.74.0Success2,228,152
Dec 4, 2023 12:53parastromparastromRUSTrust-1.74.0Error
Dec 4, 2023 12:08Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 12:05Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 11:53Sergey StreminSergey StreminRUSTrust-1.59.0Success86,945
Dec 4, 2023 11:52Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 11:50Sergey StreminSergey StreminRUSTrust-1.59.0Success86,036+0.69 RP
Dec 4, 2023 11:49Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 11:47Sergey StreminSergey StreminRUSTrust-1.59.0Success86,810
Dec 4, 2023 11:46Sergey StreminSergey StreminRUSTrust-1.59.0Error
Dec 4, 2023 11:31Sergey StreminSergey StreminRUSTrust-1.59.0Success86,579
Dec 4, 2023 11:28Sergey StreminSergey StreminRUSTrust-1.59.0Success86,688
Dec 4, 2023 11:22Sergey StreminSergey StreminRUSTrust-1.59.0Success86,552+0.08 RP
Dec 4, 2023 11:20Sergey StreminSergey StreminRUSTrust-1.59.0Success86,611+5.20 RP
Dec 4, 2023 11:13Sergey StreminSergey StreminRUSTrust-1.59.0Success91,852