why you are using two loops in here, instead it can be solved easily using 1 while loop only.
You have already sorted the array, then just compare 2 elements at a time if those two elements are equal then add that weight and increase counter by 2, if elements are not equal then just increase the counter by 1 and don’t add that weight to the ans.
N can go upto 10^5, so O(N^2) time complexity will definitely give TLE. Think of O(NlogN) or O(N) approach.
A easy to think O(N) approach is to use unordered_map and store frequency of each element, then just add el*(freq[el]/2)*2 to answer for each element. I will go with map because of trust issues with unordered_map and time complexity will be O(NlogN), still affordable.