def maxScore(popularity): N = len(popularity) popularity.sort() def cook(i=0, j=1): if i == N: return 0 return max(cook(i+1, j+1)+popularity[i]*j, cook(i+1, j)) return cook()