Simple Solution Using Map
How it work:
Init base maxSum is -1 thefore if we don't use loop then return -1 or we not found value which is contains condition

Init map - key is sum of digi
**How it work:**

1. Init base maxSum is -1 thefore if we don't use loop then return -1 or we not found value which is contains condition

```kotlin
var maxSum = -1
```
2. Init map - key is sum of digit and value is index of first occasion

```kotlin
val map = mutableMapOf<Int, Int>()
```

3. Iterate over array and caluclate sum of digit

```kotlin
val sum = sumOfDigit(nums[i])
```

4. Check if this sum contains in map
5. If key in map then take first index occasion and caluculate sum nums[first] + current value nums[i]
6. If sum is more then current then update
7. After calculate we update current index if his value is more then current store - We need store max value of pair's

```kotlin
map[sum] = when {
    nums[i] > nums[first] -> i
    else -> first
}
```

8. If don't contatins that key - we add to map
9. Return result
 
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```kotlin []
class Solution {
    fun maximumSum(nums: IntArray): Int {
        var maxSum = -1
        val map = mutableMapOf<Int, Int>()

        for (i in 0 until nums.size) {
            val sum = sumOfDigit(nums[i])
            if (map.contains(sum)) {
                val first = map.getOrDefault(sum, -1)
                maxSum = maxOf(maxSum, nums[first] + nums[i])
                map[sum] = when {
                    nums[i] > nums[first] -> i
                    else -> first
                }
            }
            else map[sum] = map.getOrDefault(sum, i)
        }

        return maxSum
    }

    private fun sumOfDigit(num: Int): Int {
        var num = num
        var res = 0
        while (num != 0) {
            res += num % 10
            num /= 10
        }

        return res
    }
}
```
['Kotlin']