-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
29 lines (29 loc) · 967 Bytes
/
Solution.cpp
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
class Solution {
public:
int maxSumMinProduct(vector<int>& nums) {
int n = nums.size();
vector<int> left(n, -1);
vector<int> right(n, n);
stack<int> stk;
for (int i = 0; i < n; ++i) {
while (!stk.empty() && nums[stk.top()] >= nums[i]) stk.pop();
if (!stk.empty()) left[i] = stk.top();
stk.push(i);
}
stk = stack<int>();
for (int i = n - 1; i >= 0; --i) {
while (!stk.empty() && nums[stk.top()] > nums[i]) stk.pop();
if (!stk.empty()) right[i] = stk.top();
stk.push(i);
}
vector<long long> s(n + 1);
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
long long ans = 0;
const int mod = 1e9 + 7;
for (int i = 0; i < n; ++i) {
long long t = nums[i] * (s[right[i]] - s[left[i] + 1]);
ans = max(ans, t);
}
return (int) (ans % mod);
}
};