Algorithms
Medium
2789
2872
Largest Element in an Array after Merge Operations
largest-element-in-an-array-after-merge-operations
"""
You are given a 0-indexed array nums consisting of positive integers.

You can do the following operation on the array any number of times:

 * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.

Return the value of the largest element that you can possibly obtain in the final array.

Example 1:

Input: nums = [2,3,7,9,3]
Output: 21
Explanation: We can apply the following operations on the array:
- Choose i = 0. The resulting array will be nums = [5,7,9,3].
- Choose i = 1. The resulting array will be nums = [5,16,3].
- Choose i = 0. The resulting array will be nums = [21,3].
The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.

Example 2:

Input: nums = [5,3,3]
Output: 11
Explanation: We can do the following operations on the array:
- Choose i = 1. The resulting array will be nums = [5,6].
- Choose i = 0. The resulting array will be nums = [11].
There is only one element in the final array, which is 11.


Constraints:

 * 1 <= nums.length <= 105
 * 1 <= nums[i] <= 106
"""
class Solution:
    def maxArrayValue(self, nums: List[int]) -> int:
        
You are given a 0-indexed array nums consisting of positive integers.

You can do the following operation on the array any number of times:

 * Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.

Return the value of the largest element that you can possibly obtain in the final array.

Example 1:

Input: nums = [2,3,7,9,3]
Output: 21
Explanation: We can apply the following operations on the array:
- Choose i = 0. The resulting array will be nums = [5,7,9,3].
- Choose i = 1. The resulting array will be nums = [5,16,3].
- Choose i = 0. The resulting array will be nums = [21,3].
The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.

Example 2:

Input: nums = [5,3,3]
Output: 11
Explanation: We can do the following operations on the array:
- Choose i = 1. The resulting array will be nums = [5,6].
- Choose i = 0. The resulting array will be nums = [11].
There is only one element in the final array, which is 11.


Constraints:

 * 1 <= nums.length <= 105
 * 1 <= nums[i] <= 106


Please complete the code below to solve above prblem:
```python
class Solution:
    def maxArrayValue(self, nums: List[int]) -> int:
        
```

my_solution = Solution()

test_input = { "nums": [2,3,7,9,3] }
assert my_solution.maxArrayValue(**test_input) == 21

test_input = { "nums": [5,3,3] }
assert my_solution.maxArrayValue(**test_input) == 11

test_input = { "nums": [77] }
assert my_solution.maxArrayValue(**test_input) == 77

test_input = { "nums": [34,95,50,12,25,100,21,3,25,16,76,73,93,46,18] }
assert my_solution.maxArrayValue(**test_input) == 623

test_input = { "nums": [40,15,35,98,77,79,24,62,53,84,97,16,30,22,49] }
assert my_solution.maxArrayValue(**test_input) == 781

test_input = { "nums": [64,35,42,19,95,8,83,89,33,21,97,11,51,93,36,34,67,53] }
assert my_solution.maxArrayValue(**test_input) == 878

test_input = { "nums": [65,68,55,6,79,30,81,25,61,2,28,59,63,15,35,8,10,83] }
assert my_solution.maxArrayValue(**test_input) == 773

test_input = { "nums": [56] }
assert my_solution.maxArrayValue(**test_input) == 56

test_input = { "nums": [100] }
assert my_solution.maxArrayValue(**test_input) == 100

test_input = { "nums": [35,23,71,38] }
assert my_solution.maxArrayValue(**test_input) == 129

test_input = { "nums": [56,67,18,81,95,41,39,56,63,70,56,31,84,46,28,38,27,56,13,10,58,16,85,21,63,8] }
assert my_solution.maxArrayValue(**test_input) == 1134

test_input = { "nums": [72,72] }
assert my_solution.maxArrayValue(**test_input) == 144

test_input = { "nums": [16,31,55] }
assert my_solution.maxArrayValue(**test_input) == 102

test_input = { "nums": [6,65,68,7,35,19,28] }
assert my_solution.maxArrayValue(**test_input) == 228

test_input = { "nums": [38,37,88,60,93,4,5,65,74,25,59,28,86,33,28,33,93] }
assert my_solution.maxArrayValue(**test_input) == 849

test_input = { "nums": [29,9,3,55,25,38,88,39,38,73,47,57,40,56,4,52,1,44,88,20,18,8] }
assert my_solution.maxArrayValue(**test_input) == 786

test_input = { "nums": [34,92,42,24,98,87,40,82,51,67,70,75,45,57,67] }
assert my_solution.maxArrayValue(**test_input) == 931

test_input = { "nums": [31,75,44,92,13,10,3,41,47,89,5,92,17,62,65,40,43,68,30,45,85,24,40,77,80,65] }
assert my_solution.maxArrayValue(**test_input) == 1218

test_input = { "nums": [63,58,61,58,82,48,83,24,24,61,31,16,26,50] }
assert my_solution.maxArrayValue(**test_input) == 685

test_input = { "nums": [10,82,74,54,20,43,74,95,17,28,44,74,25,19,75,2,84,99] }
assert my_solution.maxArrayValue(**test_input) == 919

test_input = { "nums": [91,32,21,55,44,29,82,75,66,29,77,62,55,94,49,80,12,46,80,64,88,51,2,24,11,10,86,39,16] }
assert my_solution.maxArrayValue(**test_input) == 1415

test_input = { "nums": [18,16,56,64,82,25,16,2,19] }
assert my_solution.maxArrayValue(**test_input) == 236

test_input = { "nums": [29,79,47,55,13,47,48,91,29,28,34,85,98,44,93,56,24,77,61] }
assert my_solution.maxArrayValue(**test_input) == 977

test_input = { "nums": [74,8,1,57,25,62] }
assert my_solution.maxArrayValue(**test_input) == 227

test_input = { "nums": [25,36,55,32,15,16,73,67,82,23,17,29,78,34,4,91,1,1,55,65] }
assert my_solution.maxArrayValue(**test_input) == 799

test_input = { "nums": [6,36,69,97,86,44,27,46] }
assert my_solution.maxArrayValue(**test_input) == 411

test_input = { "nums": [18,36,100,34,31,89,96,6,73,10,82,20,26,13,24,51,87,70,63,36] }
assert my_solution.maxArrayValue(**test_input) == 796

test_input = { "nums": [86,57,57,56,38,97,82,48,33,55,19,21,57,85,11,11,71,94,61,41,1,78,39,45] }
assert my_solution.maxArrayValue(**test_input) == 1243

test_input = { "nums": [33,9,11,6,68,43,76,40,91] }
assert my_solution.maxArrayValue(**test_input) == 377

test_input = { "nums": [89,45,50,98,23,79,10,98,69,65,47,46,95] }
assert my_solution.maxArrayValue(**test_input) == 814

test_input = { "nums": [58,95] }
assert my_solution.maxArrayValue(**test_input) == 153

test_input = { "nums": [91,50] }
assert my_solution.maxArrayValue(**test_input) == 91

test_input = { "nums": [99,82,49,52,5,69,65,94,94,57,46,26,28,84,42,61,19,87,71,66,1,72] }
assert my_solution.maxArrayValue(**test_input) == 1269

test_input = { "nums": [75,75,93,44,16,27,43,71,65,90,100,97,39,100,55,15,10,7,25,23,47] }
assert my_solution.maxArrayValue(**test_input) == 1117

test_input = { "nums": [65] }
assert my_solution.maxArrayValue(**test_input) == 65

test_input = { "nums": [50] }
assert my_solution.maxArrayValue(**test_input) == 50

test_input = { "nums": [56,8,10,87,83,79,33,72,32,59,75,2,46,9] }
assert my_solution.maxArrayValue(**test_input) == 594

test_input = { "nums": [26,77,78,94,90,90,57,100,60,1,98,85,78,77,63,30,88,60,41,55] }
assert my_solution.maxArrayValue(**test_input) == 1348

test_input = { "nums": [65,53,93,76,75,18,32,88,4] }
assert my_solution.maxArrayValue(**test_input) == 500

test_input = { "nums": [24,89,92,48,81,49,83,4,53,39,48,10,53,51,41,23,83,8,53,91,43,58,82] }
assert my_solution.maxArrayValue(**test_input) == 1206

test_input = { "nums": [59,17,33] }
assert my_solution.maxArrayValue(**test_input) == 59

test_input = { "nums": [15,35,97,93,34,34,90,2,21] }
assert my_solution.maxArrayValue(**test_input) == 398

test_input = { "nums": [87,64,21,27,41,63,28,75,64,22,30,76,77,91,84,81,99,86,1,74,46,4,7] }
assert my_solution.maxArrayValue(**test_input) == 1030

test_input = { "nums": [89,49,59,59,2,77,55,44,51,47,100,77,30,71,47,100,13,17,12,38,26,55,89,41] }
assert my_solution.maxArrayValue(**test_input) == 1207

test_input = { "nums": [71,4,53,51,9,92,91,86,84,58,31,39,38,49,56,27,91,17,10,56,52,78,35,76,39] }
assert my_solution.maxArrayValue(**test_input) == 1254

test_input = { "nums": [9,46,6,42,81,7,61,88,37,15,20,67] }
assert my_solution.maxArrayValue(**test_input) == 479

test_input = { "nums": [50,64,31,70,46,30,41,69,80,45,73,4,100,88,7,3,59] }
assert my_solution.maxArrayValue(**test_input) == 703

test_input = { "nums": [20,41,58,61,79,7,58,42,89,39] }
assert my_solution.maxArrayValue(**test_input) == 455

test_input = { "nums": [68,86,34,99,4,6,24,88,26,83,2,33,37,79,30,60,56,44,53,4,86,60,13,81,95,28,83,24] }
assert my_solution.maxArrayValue(**test_input) == 1362

test_input = { "nums": [5,61,59,13,21,90,32,93,84,16,71,78,53,90,5,50,47,85,83,72,88,20,97,28,73,75,59,34,21] }
assert my_solution.maxArrayValue(**test_input) == 1489

test_input = { "nums": [99,57,14,77,78,88,47,12,45,72,70,73,75,35,50,88,26,38,77,23,86,27,9,16] }
assert my_solution.maxArrayValue(**test_input) == 1230

test_input = { "nums": [68,65,95,53,51,26,2,3,17,26,15,37,50,79,20,71,99,72,82,37,29,34,74,93] }
assert my_solution.maxArrayValue(**test_input) == 1198

test_input = { "nums": [68,21,61,74,38,91,99,32,98,12,52] }
assert my_solution.maxArrayValue(**test_input) == 582

test_input = { "nums": [98,95,15,53,31,15,9,24,59] }
assert my_solution.maxArrayValue(**test_input) == 399

test_input = { "nums": [51,18,21,99,6,55,41,20,74,43,98,41,58,29,75,16,8,83,23,79,73,68,95,10,67] }
assert my_solution.maxArrayValue(**test_input) == 1174

test_input = { "nums": [78,91,52,92,42,53,77,88,40,33,86,70,85,50,65,43,75,60,28,97,95,95] }
assert my_solution.maxArrayValue(**test_input) == 1495

test_input = { "nums": [99,6] }
assert my_solution.maxArrayValue(**test_input) == 99

test_input = { "nums": [59,50,38,100,42,42,99,7] }
assert my_solution.maxArrayValue(**test_input) == 430

test_input = { "nums": [35,23,73,45,29,94,1,18,46,7,52,6,47,47,19,93,48,70,85,98,50,89,23] }
assert my_solution.maxArrayValue(**test_input) == 1075

test_input = { "nums": [2,55,19,10,28,45,86,31,45,32,38,95,65,23,50,39,51,24,40,15,16] }
assert my_solution.maxArrayValue(**test_input) == 778

test_input = { "nums": [31,59,12,90,39] }
assert my_solution.maxArrayValue(**test_input) == 192

test_input = { "nums": [53,87,11,58,79,42,44,24,68,61] }
assert my_solution.maxArrayValue(**test_input) == 466

test_input = { "nums": [20,45] }
assert my_solution.maxArrayValue(**test_input) == 65

test_input = { "nums": [85,36,99,11,91,88,55,25,68,88,27,98,7,14,40,27,18,51,90,21,77,12,87,11,37,80,70] }
assert my_solution.maxArrayValue(**test_input) == 1343

test_input = { "nums": [43,50,40,92,31,2,92] }
assert my_solution.maxArrayValue(**test_input) == 350

test_input = { "nums": [79,90,32,30,33,18,55,96] }
assert my_solution.maxArrayValue(**test_input) == 433

test_input = { "nums": [65,15,18,94,96,22,37,19,23,11,27,94,5,99] }
assert my_solution.maxArrayValue(**test_input) == 625

test_input = { "nums": [74,26,11,96,49,19,25,77,47,31,87,96,19,40,95,91,48,79,33,96] }
assert my_solution.maxArrayValue(**test_input) == 1139

test_input = { "nums": [17,90,66] }
assert my_solution.maxArrayValue(**test_input) == 107

test_input = { "nums": [60,11,95,75,10,64,62,20] }
assert my_solution.maxArrayValue(**test_input) == 166

test_input = { "nums": [99,6,67,44,84,29,87,13,44,12,92,53,26,47,88,44,75,33,19] }
assert my_solution.maxArrayValue(**test_input) == 910

test_input = { "nums": [69,21,11,10,42,3,38,36,50,28,25,93,37,45,73,37,97] }
assert my_solution.maxArrayValue(**test_input) == 715

test_input = { "nums": [32,76,65,61,84,11,94,96,17,14,79,15,62] }
assert my_solution.maxArrayValue(**test_input) == 629

test_input = { "nums": [59,91,27,74,57,30,51,67,88,26,89,10,70,31,32,26,42] }
assert my_solution.maxArrayValue(**test_input) == 870

test_input = { "nums": [8,73,22,37,39,1,66,59,5,20,16,68,55,50,48,6,8,46,93,76,48,14,92] }
assert my_solution.maxArrayValue(**test_input) == 950

test_input = { "nums": [58,34,72,5,33,34,68,96,63,85,84,74,87,33,75,43,36,28,62,44,95,39,2] }
assert my_solution.maxArrayValue(**test_input) == 1209

test_input = { "nums": [49,88,44,17,36,65,94,92,75,23,67,55,68,80,95,11,68,66,77,66,3,32,16,81,34,20,56,87,87,29] }
assert my_solution.maxArrayValue(**test_input) == 1652

test_input = { "nums": [94,27,5,47] }
assert my_solution.maxArrayValue(**test_input) == 94

test_input = { "nums": [83,85,59,55] }
assert my_solution.maxArrayValue(**test_input) == 168

test_input = { "nums": [72,56,30,65,94,91,12,99,9] }
assert my_solution.maxArrayValue(**test_input) == 519

test_input = { "nums": [9,99,20,61,57,88,50,36,21,100,62,98,94,81,96,3,98,37,88] }
assert my_solution.maxArrayValue(**test_input) == 1198

test_input = { "nums": [45,18,13,66,54,45,64,70,94,67,26,48,84,57,81,85,35,17,20,84,78,24,63,9] }
assert my_solution.maxArrayValue(**test_input) == 1238

test_input = { "nums": [5,38,82,83,92,97] }
assert my_solution.maxArrayValue(**test_input) == 397

test_input = { "nums": [95,15,19,26,59,58] }
assert my_solution.maxArrayValue(**test_input) == 214

test_input = { "nums": [94,75,16,33,2,70,56,4,64] }
assert my_solution.maxArrayValue(**test_input) == 414

test_input = { "nums": [64,50,26,66] }
assert my_solution.maxArrayValue(**test_input) == 206

test_input = { "nums": [3,48,11,71,57,72,83,61,59,25,36,29,11,69,75,48,44,44] }
assert my_solution.maxArrayValue(**test_input) == 846

test_input = { "nums": [88,7,38,15,43,8,87,7,25,2,51,29,74,34,84,87,83,34,74,22,45,96,71,4,23,28,27,68,61] }
assert my_solution.maxArrayValue(**test_input) == 1254

test_input = { "nums": [30,58,2,20,54] }
assert my_solution.maxArrayValue(**test_input) == 164

test_input = { "nums": [17,34,71,23,88,84,35,49,89,39,33,13,87,49,48,97] }
assert my_solution.maxArrayValue(**test_input) == 856

test_input = { "nums": [50,67,98,47,18,91,80,3,19,74,40,89,85,99,95,81,72,96,56,15,48,93,64] }
assert my_solution.maxArrayValue(**test_input) == 1416

test_input = { "nums": [58,10,99,6,16,94,45,47,4,30,58] }
assert my_solution.maxArrayValue(**test_input) == 467

test_input = { "nums": [92,58,90,38,37,95,47,82,6,86,99,9,91,80,73,54,45] }
assert my_solution.maxArrayValue(**test_input) == 830

test_input = { "nums": [31,100,59,88,81,74,49,21,31,53,9,89,67,4,84,46,41] }
assert my_solution.maxArrayValue(**test_input) == 840

test_input = { "nums": [25,3,94,55,70,23,43,8,65,34,83,60,53,62,97,55,3,10] }
assert my_solution.maxArrayValue(**test_input) == 775

test_input = { "nums": [27,53,99,55,15,59,85,40,46,45,45,71,42,67] }
assert my_solution.maxArrayValue(**test_input) == 749

test_input = { "nums": [8,62,12,10,79,36,59,73,76,24,45,98,72,83,61,6,19,49] }
assert my_solution.maxArrayValue(**test_input) == 872

test_input = { "nums": [2,24,30,18,94,26,22,60,50,3,27,31] }
assert my_solution.maxArrayValue(**test_input) == 387

test_input = { "nums": [64,17,57,72,24,88,29,2,23,82,15,69,80,93,38,47,9,10,68,89,65,16] }
assert my_solution.maxArrayValue(**test_input) == 976

test_input = { "nums": [99,58,59,5,67,15,6,91,71,75,79,59,40,1,18,49,48,75,92,72,81,43,31,31,29,94,39] }
assert my_solution.maxArrayValue(**test_input) == 1388