Maximize It!

  • + 0 comments
    from itertools import product
    
    K, M = list(map(int, input().split()))
    
    lists = []
    for i in range(K):
        elements = list(map(int, input().split()))
        lists.append(elements[1:])
        
    squared_totals = [[e**2 for e in lst] for lst in lists]
    squared_products = product(*squared_totals)
    
    max_s = -1
    for prod in squared_products:
        prod = sum(prod) % M
        if prod > max_s:
            max_s = prod
    print(int(max_s))