Maximize It!

  • + 0 comments
    from itertools import product
    
    K, M= list(map(int, input().split()))
    inp= [list(map(int, input().split()))[1::] for _ in range(K)]
    
    super_squared_elements = []
    for i in inp:
        squared_elements = []
        for j in i:
            squared_elements.append(j*j)
        super_squared_elements.append(squared_elements)
      
    cartesian_product= list(product(*super_squared_elements))
    
    max_check_appended= []
    
    for val in cartesian_product:
        max_check_appended.append(sum(val) % M)
    
    print(max(max_check_appended))