Solution Represent this as a Graph. Names are Nodes. Pairs of names are Edges. Generating the desired output is simply doing a BFS or DFS on each connected component, while summing the values at each node. Time/Space Complexity Same as BFS or DFS, depending on which was used