WA - need to fix
a O(n^2) solution, TLE