Maximize It!

  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from itertools import product
    
    # Input reading
    k, m = map(int, input().split())  # k = number of lists, m = modulo value
    
    # Read all lists
    lists = []
    for _ in range(k):
        data = list(map(int, input().split()))[1:]  # Skip the first number (size of list)
        lists.append(data)
    
    # Calculate the maximum value of the function using itertools.product
    max_value = 0
    for combination in product(*lists):
        # Calculate the value for the current combination
        current_value = sum(x**2 for x in combination) % m
        # Update the maximum value
        max_value = max(max_value, current_value)
    
    # Output the result
    print(max_value)