Maximize It!

  • + 0 comments
    from itertools import product
    
    if __name__ == '__main__':
        k, n = map(int, input().strip().split())
    
        arrays = []
    
        for _ in range(1, k + 1):
            arr = list(input().strip().split())[1:]
            arrays.append(arr)
    
        s_max = 0
    
        for combo in product(*arrays):
            sum_combo = sum(int(x) ** 2 for x in combo)
            current_s_max = sum_combo % n
            s_max = max(current_s_max, s_max)
    
        print(s_max)