# Arup Guha
# 3/31/2020
# Solution to COP 2930 Individual Program #5

# Did this so this program is easily extendible.
TOTALPACKS = 3

def main():

    # Get number of cards.
    nSet = int(input("How many cards in the set?\n"))

    # Read in card values.
    cardValues = []
    print("List each value, in order of card number, in cents, 1 per line.")
    for i in range(nSet):
        cardValues.append(int(input("")))

    # Here is our list of sets.
    mysets = []
    for i in range(TOTALPACKS):
        mysets.append(set())

    # Get the cards per pack.
    cardsPerPack = int(input("How many cards in a pack?\n"))

    # Ask for each set
    for i in range(TOTALPACKS):

        # Greeting.
        print("Which cards are in pack "+str(i+1)+"?")

        # Add in each card to the appropriate set.
        for j in range(cardsPerPack):
            mysets[i].add(int(input()))

    # Union of all the sets, and get their value.
    allsets = set()
    for i in range(TOTALPACKS):
        allsets = allsets | mysets[i]
    allValue = totalValue(allsets, cardValues)
                          
    # Answer for first query. (Print would change in general case.)
    print("The total value of all three packs is",allValue,"cents.")

    # Initial setting for best of two packs.
    pack1 = 1
    pack2 = 2
    value = totalValue(mysets[0] | mysets[1], cardValues)
                          
    # This is going through all pairs.
    for i in range(TOTALPACKS):
        for j in range(i+1, TOTALPACKS):

            # Get the value of these two packs.
            tmpValue = totalValue(mysets[i] | mysets[j], cardValues)

            # This is better, so update everything.
            if tmpValue > value:
                value = tmpValue
                pack1 = i+1
                pack2 = j+1

    # Report the best two pack combo.
    print("The two packs with most value are",pack1,"and",pack2,"worth",value,"cents.")
    

# Pre-condition: setOfCards is a set of integers in between 0 and n-1,
# where n is the length of the list listOfCardValues, and
# listOfCardValues[i] is the value of card number i.
# Post-condition: Returns the total value of the cards specified by the set setOfCards.
def totalValue(setOfCards, listOfCardValues):

    total = 0

    # Go through each card in the set.
    for x in setOfCards:

        # Add its value to our accumulator.
        total += listOfCardValues[x]

    # Ta da!
    return total

# Do it!
main()
