1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public long maximumSumOfHeights(List<Integer> nums) { int n = nums.size(); long res = 0; for (int i = 0; i < n; ++i) { int x = nums.get(i), pre = x; long sum = x; for (int j = i - 1; j >= 0; --j) { int y = nums.get(j); if (y > pre) y = pre; sum += y; pre = y; } pre = x; for (int j = i + 1; j < n; ++j) { int y = nums.get(j); if (y > pre) y = pre; sum += y; pre = y; } res = Math.max(res, sum); } return res; } }
|