Maximize It!

Sort by

recency

|

1087 Discussions

|

  • + 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)
    
  • + 0 comments
    from itertools import product
    def f(x):
        return int(x)**2
    a, b = tuple(list(map(int, input().split())))
    pro = []
    for i in list(product(*[list(map(f, input()[1:].split())) for _ in range(a)])):
        pro.append(sum(i) % b)
    print(max(pro)) 
    
  • + 0 comments
    from itertools import product
    
    K, M = list(map(int, input().split()))
    
    lists = []
    for i in range(K):
        elements = list(map(int, input().split()))
        lists.append(elements[1:])
        
    squared_totals = [[e**2 for e in lst] for lst in lists]
    squared_products = product(*squared_totals)
    
    max_s = -1
    for prod in squared_products:
        prod = sum(prod) % M
        if prod > max_s:
            max_s = prod
    print(int(max_s))