CF1209E2 Rotate Columns (hard version)

Description

This is a harder version of the problem. The difference is only in constraints. You are given a rectangular $ n \times m $ matrix $ a $ . In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times. After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $ i $ -th row it is equal $ r_i $ . What is the maximal possible value of $ r_1+r_2+\ldots+r_n $ ?

Input Format

N/A

Output Format

N/A

Explanation/Hint

In the first test case you can shift the third column down by one, this way there will be $ r_1 = 5 $ and $ r_2 = 7 $ . In the second case you can don't rotate anything at all, this way there will be $ r_1 = r_2 = 10 $ and $ r_3 = 9 $ .