C++ |EASY EXPLANATION| O(1) SPACE| O(nlog(n)) TIME
Approach\n1. SORTING ARRAY AND TRAVERSING FROM RIGHT TO LEFT TO ENSURE THAT IF THERE ARE TWO ELEMENTS OF SAME SUM OF DIGITS ,THEN THEIR SUM WILL BE MAXIMUM .\n2
# Approach\n1. SORTING ARRAY AND TRAVERSING FROM RIGHT TO LEFT TO ENSURE THAT IF THERE ARE TWO ELEMENTS OF SAME SUM OF DIGITS ,THEN THEIR SUM WILL BE MAXIMUM .\n2. FOR KNOWING WE GOT TWO ELEMENTS ,AND THERE IS NO NEED TO ADD FURTHER MORE ELEMENTS .\nWE WILL APPLY A CHECK . AND STORE THE CONTRIBUTION OF ELEMENT IN AN \nans VECTOR {SIZE 82 (STORING 0 TO 81 INDEXES). HERE INDEX REPRESENT SUM OF DIGITS OF A NUMBER .}\n\n        if(check[x]<2){\n            check[x]=check[x]+1;\n            ans[x]+=nums[i];\n        }\n\n3.WE WILL PUSH THE ANS[X] INTO THE STACK ST ONLY  IF WE FOUND THE TWO DISTINCT ELEMENTS IN THE NUMS ARRAY HAVING SAME SUM OF DIGITS \nAND\nI.STACK ST IS EMPTY .\nII.ST.TOP() < ANS[X]{i.e. FOR STORING MAX SUM OF PAIR .}\n\n  if(check[x]==2){\n            if(st.empty()){st.push(ans[x]);}\n            else if(st.top()<ans[x]){\n                st.push(ans[x]);\n            }\n        }\n\n\n# Complexity\n- Time complexity:\nO(nlogn) --> sorting nums\n\n  O(1) --> sumofDigits will run at max 9 times.\n O(1) --> Stack push / pop .\n\n- Space complexity:\nO(1) --> check {82 size}\nO(1) --> ans {82 size}\nO(1) --> st {Only 1 element or no element will there in stack for whole iteration.}\n\n# Code\n```\nclass Solution {\npublic:\n    int sumofDigits(int n){\n        long long sum=0;\n        while(n!=0){\n            sum+=(n%10);\n            n/=10;\n        }\n        return sum;\n    }\n\n    int maximumSum(vector<int>& nums) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        vector<int>check(82,0);\n        vector<int>ans(82,0);\n        stack<int>st;\n\n        for(int i=n-1;i>=0;i--){\n            int x=sumofDigits(nums[i]);\n            if(check[x]<2){\n                check[x]=check[x]+1;\n                ans[x]+=nums[i];\n            }\n            if(check[x]==2){\n                if(st.empty()){st.push(ans[x]);}\n\n                else if(st.top()<ans[x]){\n                    st.push(ans[x]);\n                }\n            }\n        }\n        if(st.empty())return -1;\n        return st.top();\n    }\n};\n```\n\n![UPVOTE.jpeg](https://assets.leetcode.com/users/images/a809c084-cccb-423d-9d90-b45405f3ea74_1676003023.5817654.jpeg)\n
['C++']