思路
枚举当每一个数为序列的最小值的时候,取该情况下al~ar的和的最大值,遍历n次,之后选所有数据中的最大值就行了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> v;
vector<int> sum;
vector<int> L;
vector<int> R;
int main(void) {
int n;
cin >> n;
while (n--) {
int m;
cin >> m;
v.push_back(m);
}
sum.push_back(v[0]);
for (int i = 1; i < v.size(); i++) {
sum.push_back(sum[i - 1] + v[i]);
}
for (int i = 0; i < v.size(); i++) {
L.push_back(i);
R.push_back(i);
}
for (int i = 0; i < v.size(); i++) {
for (int j = i+1; j < v.size(); j++) {
if (v[i] <= v[j])
{
R[i] = j;
}
else
{
break;
}
}
for (int j = i -1; j >=0; j--) {
if (v[i] <= v[j])
{
L[i] = j;
}
else
{
break;
}
}
}
int ans = 0;
for (int i = 0; i < v.size(); i++) {
int tmp = (sum[R[i]] - sum[L[i]]+v[L[i]]) * v[i];
if (tmp > ans)
ans = tmp;
cout << tmp << " ";
}
cout << ans;
system("pause");
}