title: Hackerrank | Maximize it
body: <p>You are given a function f(X)=X^2.</p>
  
  <p>You are also given K lists. The  list consists of Ni elements.</p>
  
  <p>You have to pick exactly one element from each list so that the equation below is maximized: </p>
  
  <p>S=(f(X1)+f(X2)+...+f(Xk))%M</p>
  
  <p>Xi denotes the element picked from the ith list . Find the maximized value Smax obtained.</p>
  
  <p>% denotes the modulo operator.</p>
  
  <p>This is the code that I have written.</p>
  
  <pre><code>n=list(map(int,raw_input().split()))
  l=n[0]
  div=n[1]
  b=list()
  for i in range(0,l):
      a=list()
      a.extend(map(int,raw_input().split()))
      b.append(max(a))
      su=0
  for j in b:
      su+=j*j
  
  print su%div
  </code></pre>
  
  <p>I am not sure why the code is failing for some test cases.I am not sure where I am going wrong.</p>
  
  <p>I saw some answer in which they were using Cartesian product.</p>
  
  <pre><code>import itertools
  
  k, m = map(int, raw_input().strip().split(' '))
  a = []
  for i in range(k):
      a.append(raw_input().split(' ')[1:])
  
  mx = 0
  for tp in itertools.product(*a):
      res = sum([int(x)**2 for x in tp]) % m
      if res &gt; mx:
          mx = res
  
  print mx
  </code></pre>