Maximize It!

  • + 0 comments

    More clear solution

    from itertools import product
    k, m = list(map(int, input().split()))
    
    # f(X) function
    def f(x):
        return x**2
    
    lst = []
    for _ in range(k):
        local_input = list(map(int, input().split()))
        lst.append(local_input[1:])
    
    # Apply f(X) function
    sqared_list = [[f(x) for x in l] for l in lst]
    
    # Generate cartesian product to obtain all possible combinations
    combined = product(*sqared_list)
    max_s = 0
    for c in combined:
        # Get local sum
        local_sum = sum(c)
        
        # Get local mod of m
        local_mod = local_sum % m
        
        # Get the maximized value
        max_s = max(max_s, local_mod)
    print(max_s)