Maximize It!

Sort by

recency

|

1089 Discussions

|

  • + 0 comments

    was a bit tough and fun

    from itertools import product
    
    k, m = map(int, input().split())
    L, max_value = [], 0
    for i in range(0, k):
        L.append(list(map(lambda x: int(x) ** 2, input().split()[1:])))
    
    for i in product(*L):
        if max_value < (sum(i) % m):
            max_value = sum(i) % m
    print(max_value)
    
  • + 1 comment

    unable to pass 4 test cases any help in logic would be appriciated

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import itertools
    k_n_list=input().split()  #take K,M as input
    number=int(k_n_list[0])   # getting number of K
    new_list=[]               # empty list 
    find_element=[]           # empty list to store all possible squared modulo operated value
    for i in range(0,number):   # for loop for appending the number of inputed list
        new_list.append(input().split())  # nested list of lists
    combinations = list(itertools.product(*new_list))  # all possible values of the nested list
    # it dose not conatain the same element from the list 
    for comb in combinations: # square and modulo operation in this loop
        b =list((map(int, comb)))
        out=sum(map(lambda i: i*i , b))% int(k_n_list[1])
        find_element.append(out)
    print(max(find_element))
    
  • + 0 comments

    import itertools

    user_input = input().split()

    k = int(user_input[0]) m = int(user_input[1])

    lists = []

    for _ in range(k): lists.append([int(x) for x in input().split()][1:])

    combinations = list(itertools.product(*lists))

    squared_combinations = [[x**2 for x in combination] for combination in combinations]

    s_max = []

    for squared_combination in squared_combinations: if (sum(squared_combination) % m > sum(s_max) % m): s_max = squared_combination

    print(sum(s_max) % m)

  • + 0 comments

    It took me more than expected time, around 2 hrs for me but heres raw version without any extra dependancies.

    line1 = input().strip().split() K = int(line1[0]) M = int(line1[1])

    values = {} elCounts = {} for i in range(K): values[i] = [] line = input().strip().split() for j in range(int(line[0])): n = int(line[j+1]) values[i].append(n * n) elCounts[i] = len(values[i])

    out = [] maxVal = 0

    def myfunc(n): return n[1]

    ascEl = [ e[0] for e in sorted(elCounts.items(), key=myfunc)]

    def recusiveP(v, keys, maxEls, arr): global M global maxVal k = keys[0]

    for n in arr[k]:
        if(len(keys) > 1):
            vals = arr.copy()
            del vals[k]
            recusiveP(v+n, keys[1:], maxEls, vals)
        else:
            for n in arr[k]:
                v1 = (v + n) % M
    
                if(v1 > maxVal):
                    maxVal = v1
    return             
    

    recusiveP(0, ascEl, elCounts, values) print(maxVal)

    Your views are welcome...

  • + 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)