2342.cpp (575B)
1 class Solution { 2 static int sum_digits(int n) { 3 int res = 0; 4 while (n) 5 res += n % 10, n /= 10; 6 return res; 7 } 8 9 public: 10 int maximumSum(const vector<int> &nums) const { 11 static int um[82]; 12 int res = -1; 13 14 memset(um, 0x00, sizeof(um)); 15 for (const int num : nums) { 16 const int sum = sum_digits(num); 17 if (um[sum]) 18 res = max(res, num + um[sum]), um[sum] = max(um[sum], num); 19 else 20 um[sum] = num; 21 } 22 23 return res; 24 } 25 };