# Arup Guha
# 4/8/2020
# Alternate Solution to COP 2930 Individual Program #5
# Only works for three packs.

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.
    pack1 = set()
    pack2 = set()
    pack3 = set()

    # Get the cards per pack.
    cardsPerPack = int(input("How many cards in a pack?\n"))

    # Greeting.
    print("Which cards are in pack 1?")

    # Add in each card to set 1.
    for i in range(cardsPerPack):
        pack1.add(int(input()))

    # Greeting.
    print("Which cards are in pack 2?")

    # Add in each card to set 2.
    for i in range(cardsPerPack):
        pack2.add(int(input()))

    # Greeting.
    print("Which cards are in pack 3?")

    # Add in each card to set 3.
    for i in range(cardsPerPack):
        pack3.add(int(input()))         

    # Union of all the sets, and get their value.
    allsets = pack1 | pack2 | pack3
    allValue = totalValue(allsets, cardValues)
                          
    # Answer for first query.
    print("The total value of all three packs is",allValue,"cents.")

    # Calculate values for all possible pairs of packs.
    val12 = totalValue(pack1 | pack2, cardValues)
    val13 = totalValue(pack1 | pack3, cardValues)
    val23 = totalValue(pack2 | pack3, cardValues)

    # Report the best two pack combo by checking all cases.
    if val12 > val13 and val12 > val23:
        print("The two packs with most value are 1 and 2 worth",val12,"cents.")
    elif val13 > val12 and val13 > val23:
        print("The two packs with most value are 1 and 3 worth",val13,"cents.")
    else:
        print("The two packs with most value are 2 and 3 worth",val23,"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

# Go!
main()
