TOWIN, Winning Stratergy

Hi, I was doing this question. But after lots of tries, I was not able to solve it. Then I read the editorial where they mentioned sorting and find maximum sum then.
My question is that the question was so obvious that I didn’t get the idea of sorting. So where have I done the mistake while reading the question or am I doing it wrong all the way without sorting which I definitely was.
But I didn’t get the intuition of sorting.
what should I do?

Thank you
Gaurav Gupta