You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
typedef long long ll;
struct node
{
int l, r;
ll num;
} s[maxn];
ll a[maxn];
ll sum[maxn];
ll ans;
int ans_l, ans_r;
void update(int top)
{
int l = s[top].l, r = s[top].r;
if ((sum[r] - sum[l - 1]) * s[top].num > ans)
{
ans = (sum[r] - sum[l - 1]) * s[top].num;
ans_l = l;
ans_r = r;
}
if (top > 0)
s[top - 1].r = s[top].r;
}
ll main2(int n)
{
ans = -1;
int top = -1;
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
//scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
for (int i = 1; i <= n; i++)
{
node v = {i, i, a[i]};
while (top != -1 && s[top].num >= a[i])
{
update(top);
v.l = s[top].l;
top--;
}
s[++top].l = v.l;
s[top].r = v.r;
s[top].num = v.num;
}
while (top != -1)
{
update(top);
top--;
}
if (1)
{
printf("checkseq:");
for (int i = 1; i <= n; i++)
printf("%d ", a[i]);
printf("\nres: %lld\n", ans);
}
return ans;
}
int a2[maxn];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", a2 + i);
ll ans = 0;
int last = 0;
for (int i = 1; i <= n; i++)
{
if (1ll * a2[i] * a2[i - 1] < 0)
{
for (int j = 1; j <= (i - last); j++) a[j] = abs(a2[last + j - 1]);
ans = max(ans, main2(i - last));
last = i;
}
}
for (int j = 1; j <= (n - last); j++) a[j] = abs(a2[last + j - 1]);
ans = max(ans, main2(n - last));
printf("%lld", ans);
return 0;
}