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.
# Reading each list
for _ in range(K):
data = list(map(int, input().split()))
lists.append(data[1:]) # Skip the first element as it's the size N
# Generate all possible combinations (one element from each list)
max_s = 0 # To track the maximum S
# Iterate through all combinations
for combo in itertools.product(lists):
# Calculate S for the current combination
s = sum(x * 2 for x in combo) % M
# Update the maximum S if needed
max_s = max(max_s, s)
Output the result
print(max_s)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximize It!
You are viewing a single comment's thread. Return to all comments →
import itertools
# Read input K, M = map(int, input().split()) lists = []
# Reading each list for _ in range(K): data = list(map(int, input().split())) lists.append(data[1:]) # Skip the first element as it's the size N
# Generate all possible combinations (one element from each list) max_s = 0 # To track the maximum S
# Iterate through all combinations for combo in itertools.product(lists): # Calculate S for the current combination s = sum(x * 2 for x in combo) % M # Update the maximum S if needed max_s = max(max_s, s)
Output the result
print(max_s)