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 listApr 27, 2026 21:21TudyMTudyMError
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Apr 27, 2026 21:21017,541,12001,538,0002,375,680expected "1877514 ", got "+ 988 24|+ 995 494|+ 1016 498|+ 1109 51|+ 995 138|+ 1087 98|+ 904 298|+ 994 157|+ 920 146|+ 1110 309|+ 978 375|+ 1094 85|+ 1004 487|+ 1102 467|+ 1000 326|+ 896 72|+ 852 246|+ 1238 223|+ 911 189|+ 1093 455|+ 1013 30|+ 994 297|+...
2Apr 27, 2026 21:21024,170,64401,606,0002,375,680expected "3097749 ", got "+ 897 294|+ 1161 197|+ 875 308|+ 1027 112|+ 1036 153|+ 813 370|+ 1034 90|+ 897 124|+ 1089 415|+ 1289 239|+ 1107 57|+ 1012 95|+ 991 401|+ 931 268|+ 762 351|+ 989 9|+ 1058 175|+ 1113 56|+ 921 476|+ 1060 429|+ 917 365|+ 917 144|+...
3Apr 27, 2026 21:21018,296,57401,553,0002,367,488expected "1247361 ", got "+ 1225 355|+ 1084 101|+ 940 16|+ 1043 210|+ 849 193|+ 951 461|+ 998 57|+ 978 168|+ 1042 376|+ 902 364|+ 968 355|+ 984 203|+ 896 463|+ 1032 13|+ 1111 282|+ 1148 39|+ 931 431|+ 1178 225|+ 896 407|+ 983 352|+ 962 154|+ 949 48|+ 1...