---
# System prepended metadata

title: 2789. Largest Element in an Array after Merge Operations
tags: [Leetcode, Medium, Greedy]

---

Link: https://leetcode.com/problems/largest-element-in-an-array-after-merge-operations/description/
## 思路
试想三个non-decreasing的element在一块 我们应该如何合并
显然我们应该先合并后两个 再和第一个合并
所以我们从后往前遍历 合并所有能合并的element
就是最优的合并方法
## Code
```python=
class Solution:
    def maxArrayValue(self, nums: List[int]) -> int:
        curr, maxVal = -math.inf, 0
        for i in range(len(nums)-1, -1, -1):
            if curr>=nums[i]:
                curr += nums[i]
            else: curr = nums[i]
            maxVal = max(curr, maxVal)
        return maxVal
```