-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3640.cpp
More file actions
45 lines (42 loc) · 1.41 KB
/
3640.cpp
File metadata and controls
45 lines (42 loc) · 1.41 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
long long maxSumTrionic(vector<int>& nums) {
int n = nums.size();
vector<pair<int, int>> ps;
int index = 0;
while (index < n) {
int left = index;
while (index + 1 < n && nums[index] > nums[index + 1]) index++;
if (index > left) {
ps.push_back({left, index});
}
index++;
}
long long res = LLONG_MIN;
for (auto [l, r] : ps) {
long long midSum = accumulate(nums.begin() + l, nums.begin() + r + 1, 0ll);
if (l - 1 < 0) continue;
if (r + 1 >= n) continue;
if (nums[l] == nums[l - 1]) continue;
if (nums[r] == nums[r + 1]) continue;
long long leftSum = nums[l - 1];
long long maxLeftSum = leftSum;
l--;
while (l - 1 >= 0 && nums[l] > nums[l - 1]) {
l--;
leftSum += nums[l];
maxLeftSum = max(maxLeftSum, leftSum);
}
long long rightSum = nums[r + 1];
long long maxRightSum = rightSum;
r++;
while (r + 1 < n && nums[r + 1] > nums[r]) {
r++;
rightSum += nums[r];
maxRightSum = max(maxRightSum, rightSum);
}
res = max(res, maxLeftSum + maxRightSum + midSum);
}
return res;
}
};