Sharp bounds for sums associated to graphs of matrices