Maximize It! ⬀
You are given a function f(X) = X². You are also given K lists. The iᵗʰ list consists of Nᵢ elements.
You have to pick one element from each list so that the value from the equation below is maximized:
S = (f(X₁) + f(X₂) + ... + f(Xₖ)) % M
Xᵢ denotes the element picked from the iᵗʰ list . Find the maximized value Sₘₐₓ obtained.
% denotes the modulo operator.
Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen elements and perform the modulo operation. The maximum value that you can obtain, will be the answer to the problem.
- The first line contains
2space separated integersKandM. - The next
Klines each contains an integerNᵢ, denoting the number of elements in theiᵗʰlist, followed byNᵢspace separated integers denoting the elements in the list.
1 ≤ K ≤ 71 ≤ M ≤ 10001 ≤ Nᵢ ≤ 71 ≤ Magnitude of elements in list ≤ 10⁹
Output a single integer denoting the value Sₘₐₓ.
3 1000
2 5 4
3 7 8 9
5 5 7 8 9 10
206
Picking 5 from the 1ˢᵗ list, 9 from the 2ⁿᵈ list and 10 from the 3ʳᵈ list gives the maximum S value equal to (5² + 9² + 10²) % 1000 = 206.