Maximize It!

Sort by

recency

|

1107 Discussions

|

  • + 0 comments

    After several try, I finally pass this Hard problem with the following code. It's maybe possible to improve it in term of time complexity. The logic is to take all product possibilities using the itertool.product() method, then, with a temp value, see if the current result is better than the output.

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from itertools import product
    
    K, M = map(int, input().split())
    
    all_lists = []
    
    for i in range(K):
        all_lists.append(list(map(int, input().split()[1:])))
    
    #print(all_lists)
    
    for sub_list in all_lists:
        sub_list.sort() 
    
    
    
    #all_product_possibilities = list(product(*all_lists))
    # print(all_product_possibilities[:5])
    
    output = 0
    
    #print("-------------------------")
    
    for possible_product in list(product(*all_lists)):
        current_product = list(possible_product)
        #print(f"current_product = {current_product}")
        temp = 0
        for values in current_product:
            temp += values**2
        
        temp = temp%M
        #print(f"Temp: {temp}")
        #print(f"Output: {output}")
        if temp > output:
            output = temp
        else:
            continue
        #print(f"Current Output = {output}")
    print(output)
            
    
  • + 0 comments
    from itertools import product
    
    K, M= list(map(int, input().split()))
    inp= [list(map(int, input().split()))[1::] for _ in range(K)]
    
    super_squared_elements = []
    for i in inp:
        squared_elements = []
        for j in i:
            squared_elements.append(j*j)
        super_squared_elements.append(squared_elements)
      
    cartesian_product= list(product(*super_squared_elements))
    
    max_check_appended= []
    
    for val in cartesian_product:
        max_check_appended.append(sum(val) % M)
    
    print(max(max_check_appended))
    
  • + 0 comments
    from itertools import product
    
    K, M = map(int, input().split())
    a = [list(map(int, input().split()))[1:] for _ in range(K)]
    
    print(max([sum(map(lambda x: x**2, combo)) % M for combo in product(*a)]))
    
  • + 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)
    
  • + 0 comments

    Solution

    from itertools import product
    
    k,m = map(int, input().split())
    listy = []
    
    for _ in range(k):
        listy.append(list(map(int, input().split()))[1:])
    
    najw = 0
    for i in product(*listy):
        wartosc = sum(x**2 for x in i)%m
        najw = max(najw, wartosc)
    
    print(najw)