CF802C Heidi and Library (hard)

Description

The good times at Heidi's library are over. Marmots finally got their internet connections and stopped coming to the library altogether. Not only that, but the bookstore has begun charging extortionate prices for some books. Namely, whereas in the previous versions each book could be bought for 1 CHF, now the price of book $ i $ is $ c_{i} $ CHF.

Input Format

N/A

Output Format

N/A

Explanation/Hint

The first three sample cases are repeated, but the fourth one is new. In the fourth test case, when buying book $ 3 $ , Heidi should discard either book $ 1 $ or $ 2 $ . Even though book $ 2 $ will be requested later than book $ 1 $ , she should keep it, because it is so expensive to buy again.