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.

Back to listJan 19, 2025 23:31alxmrcktralxmrcktrError
Source Code

Source code access is restricted. Log in to request access.

Challenge History
DateChallengerCostTypeStatus
Oct 5, 2026 18:38Andrew Epstein10.00CustomSolution Broken
Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 5, 2026 18:420357,328,426343,826,0003,007,0002,117,632expected "861892207 ", got "2296324 "
2Oct 5, 2026 18:420357,974,571344,683,0003,008,0002,117,632expected "862904240 ", got "2250971 "
3Oct 5, 2026 18:420358,024,567344,733,0003,012,0002,117,632expected "863921908 ", got "2250971 "
4Jan 19, 2025 23:31255,897177,471,930148,420,00002,289,664
5Jan 19, 2025 23:31256,826168,337,095145,960,0002,999,0002,351,104
6Jan 19, 2025 23:31257,498175,656,461148,347,0001,002,0002,347,008
7Jan 19, 2025 23:31257,536172,457,928146,364,0003,007,0002,347,008
8Jan 19, 2025 23:31257,669172,670,903147,442,0002,006,0002,355,200
9Jan 19, 2025 23:31257,691169,308,692149,461,00002,289,664
10Jan 19, 2025 23:31257,778173,343,192148,508,0001,003,0002,289,664
11Jan 19, 2025 23:31258,491175,907,332148,926,000999,0002,342,912
12Jan 19, 2025 23:31277,902189,365,197158,180,0003,003,0002,289,664