


A section is considered painted if at least one painter paints it.
The first line of the input contains two positive integers ‘N’ and ‘Q’ which represent the length of the fence and number of painters, respectively.
From the second line, the next ‘Q’ lines represent the range of sections of the fence that i-th painter can paint. Every range contains two single space-separated integers representing 'Li' and 'Ri', respectively.
The only line of output will print an integer denoting the maximum number of painted sections if you hire 'Q' - 2 painters optimally.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
3 <= N <= 1000
3 <= Q <= 500
1 <= Li <= N
Li <= Ri <= N
Time limit: 1 sec
We will consider each pair of painters using two nested loops and count all the sections that can be painted by removing each pair of painters.
Out of all the pairs, we will pick one which will result in maximum painted sections.
The problem boils down to finding the sections that could remain unpainted if we remove 2 painters and then subtracting the minimum of them from the total sections that can be painted by all painters.
5. Finally, Return ‘TOTAL’ - minimum of ‘COUNT’ array/list.
In this approach, we will try to optimize approach 1. We will use the prefix sum array to reduce the time complexity.