CF1251E2 Voting (Hard Version)
Description
The only difference between easy and hard versions is constraints.
Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.
There are $ n $ voters, and two ways to convince each of them to vote for you. The first way to convince the $ i $ -th voter is to pay him $ p_i $ coins. The second way is to make $ m_i $ other voters vote for you, and the $ i $ -th voter will vote for free.
Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $ m_1 = 1 $ , $ m_2 = 2 $ , $ m_3 = 2 $ , $ m_4 = 4 $ , $ m_5 = 5 $ , then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: $ {5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5} $ .
Calculate the minimum number of coins you have to spend so that everyone votes for you.
Input Format
N/A
Output Format
N/A
Explanation/Hint
In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: $ {3} \rightarrow {1, 3} \rightarrow {1, 2, 3} $ .
In the second example you don't need to buy votes. The set of people voting for you will change as follows: $ {1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7} $ .
In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: $ {2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6} $ .