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 index
isuch that0 <= i < nums.length - 1andnums[i] <= nums[i + 1]. Replace the elementnums[i + 1]withnums[i] + nums[i + 1]and delete the elementnums[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
21The operations can produce
[21, 3], so the largest element in the final array is 21, and no larger element can be obtained.Example 2
Input
nums = [5,3,3]Output
11After merging at
i = 1 and then at i = 0, the final array is [11].Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^6