There are 2 criteria to make a difficult password:
1) Choose M numbers out of N given numbers:
s1 = summation of differences between every 2 adjacent M numbers.
For example: if the M numbers are: 2,5,8:
s1=5−2+8−5=6.
2) For given (L,R):
s2 = numbers in the interval [L,R] (both inclusive) that don't have divisors in the M numbers.
Write a program to choose M numbers out of N numbers to maximise the summation
of (s1+s2).
Input Format
First line contain T, that denotes number of test cases.
For each test case:
Second line contains 2 integers (N,M).
Third line contains 2 integers (L,R).
Fourth line contains N numbers.
Output Format
For each test case print the maximum value of (s1+s2) that could be achieved.
Input Constraints
1≤T≤50
1≤M≤N≤14
1≤L≤R≤109
1≤ai≤109
The Optimal solution is to choose (3,7), M=2.
Result = difference between adjacent numbers + numbers that don't have divisors in the M numbers = 4 + 6 = 10.
Numbers that don't have divisors in the M numbers = (20,22,23,25,26,29).