The only difference between easy and hard versions is a number of elements in the array. You are given an array consisting of integers. The value of the -th element of the array is . You are also given a set of segments. The -th segment is , where . You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array and the given segments are and then you can choose both of them and the array will become . You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array and obtain the array then the value will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. ### Input: The first line of the input contains two integers and () — the length of the array and the number of segments, respectively. The second line of the input contains integers (), where is the value of the -th element of the array . The next lines are contain two integers each. The -th of them contains two integers and (), where and are the ends of the -th segment.
Output
In the first line of the output print one integer — the maximum possible value if is the array obtained by applying some subset of the given segments to the array . In the second line of the output print one integer () — the number of segments you apply. In the third line print distinct integers in any order () — indices of segments you apply to the array in such a way that the value of the obtained array is maximum possible. If there are multiple answers, you can print any.