def top_files(query, files, idfs, n):
    """
    Given a `query` (a set of words), `files` (a dictionary mapping names of
    files to a list of their words), and `idfs` (a dictionary mapping words
    to their IDF values), return a list of the filenames of the the `n` top
    files that match the query, ranked according to tf-idf.
    """
    tf_idf=dict()
    for file in files:
        tf_idf_values =0
        for word in query:
            tf = files[file].count(word)
            if word in idfs:
                tf_idf_values += (tf * idfs[word])
            else:
                print("##Please Note: Some words in the query were not present in the idfs.")
        tf_idf[file]=tf_idf_values

    return sorted(tf_idf, key=lambda x: tf_idf[x], reverse=True)[:n]