We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Itertools
- Maximize It!
- Discussions
Maximize It!
Maximize It!
Sort by
recency
|
1087 Discussions
|
Please Login in order to post a comment
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)
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]
recusiveP(0, ascEl, elCounts, values) print(maxVal)
Your views are welcome...