You have to construct a bridge in a city. Bridge consists of n sections (1 to n). There are m construction teams. The Ith team will work on the section from (li to ri) of the bridge. Bridge is said to be constructive if all sections from (1 to n) are worked on by at least one of the teams.
The cost of building a section is number of teams that worked on it, So the cost of building the bridge is the sum of the cost of cost of building all the sections.
Your task is to choose a subset of the teams such that bridge is completely constructed(i.e all the sections from (1 to n) is worked on by at least one of the team from this subset) and cost of building the bridge is the minimum possible. If it is not possible, print -1.
Examples:
N=4 m=3; teams = [(1,2),(2,3),(3,4)]
Approach :
- The optimal way is to choose team 1 and team 3. 2) Sections 1 and 2 will be constructed by team 1 and sections 3 and 4 will be by team 3
- so cost of constructing the bridge will be 4, as each section is worked on exactly 1 team.
Hence answer is 4.
Please provide the efficient approach for this problem.