Maximize It!

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