Solve Q85 in O(n^2)