import fileinput from collections import defaultdict def work_seconds(node): return ord(node) - ord('A') + 60 graph = defaultdict(set) for line in fileinput.input(): a = line[5] b = line[36] graph[b].add(a) graph[a] order = [] to_consider = sorted(graph.keys()) while to_consider: for i, node in enumerate(to_consider): parents = graph[node] if any(parent in to_consider for parent in parents): continue order.append(node) del to_consider[i] break print("".join(order))