Maximize It!

  • + 0 comments
    from itertools import product
    
    def f(X,m):
        result = 0
        for element in X:
            result += element**2
        return result%m
        
        
    def get_max_combo():
        num_lists, m = map(int, input().split()) # Read
        lists = [] # initialize where to save in the lists
        # save lists from input into lists
        for i in range(num_lists): 
            num_elements, *elements = map(int, input().split())
            lists.append(elements)
        # create combinations of every possible list entry with entries from other list entries 
        combos = list(product(*lists))
        max_f = 0
        for combo in combos:
            current_max = f(combo, m)
            if current_max > max_f:
                max_f = current_max
        print(max_f)
        
        
    get_max_combo()