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.

96 lines
2.8 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
typedef long long ll, i64;
template <size_t sz>
struct SegTree
{
#define L(x) ((x) << 1)
#define R(x) ((x) << 1 | 1)
#define avg(x, y) (((x) + (y)) >> 1)
struct Node
{
ll sum, lmx, lmn, rmx, rmn;
void init(const int x) { sum = x, lmx = x, lmn = x, rmx = x, rmn = x; }
} N[sz << 2];
void merge(Node &c, Node &l, Node &r)
{
c.sum = l.sum + r.sum;
c.lmn = min(l.lmn, l.sum + r.lmn);
c.rmn = min(r.rmn, r.sum + l.rmn);
c.lmx = max(l.lmx, l.sum + r.lmx);
c.rmx = max(r.rmx, r.sum + l.rmx);
}
void build(int k, int l, int r, int *a)
{
if (l == r - 1) return N[k].init(a[l]);
int m = avg(l, r);
build(L(k), l, m, a);
build(R(k), m, r, a);
merge(N[k], N[L(k)], N[R(k)]);
}
Node query(int k, int l, int r, int x, int y)
{
if (x >= y) return {};
if (x <= l && r <= y) return N[k];
int m = avg(l, r);
if (y <= m) return query(L(k), l, m, x, y);
if (x >= m) return query(R(k), m, r, x, y);
Node res[3];
res[1] = query(L(k), l, m, x, y);
res[2] = query(R(k), m, r, x, y);
merge(res[0], res[1], res[2]);
return *res;
}
};
template <typename T>
inline void read(T &x)
{
int ch = x = 0, sgn = 1;
while (!isdigit(ch = getchar()))
if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
x *= sgn;
}
const int N = 3e6 + 50, inf = 0x3f3f3f3f;
SegTree<N> st;
int a[N], b[N], l[N], r[N];
vector<int> v;
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 1; i <= n; i++) read(b[i]);
a[0] = a[n + 1] = -inf;
v.clear(), v.push_back(0);
for (int i = 1; i <= n; i++)
{
while (a[v.back()] >= a[i]) v.pop_back();
l[i] = v.back(), v.push_back(i);
}
v.clear(), v.push_back(n + 1);
for (int i = n; i >= 1; i--)
{
while (a[v.back()] >= a[i]) v.pop_back();
r[i] = v.back(), v.push_back(i);
}
st.build(1, 0, n, b + 1);
long long ans = 0;
for (int i = 1; i <= n; i++)
{
auto resl = st.query(1, 0, n, l[i], i - 1);
auto resr = st.query(1, 0, n, i, r[i] - 1);
ans = max(ans, a[i] < 0 ? a[i] * (resl.rmn + b[i] + resr.lmn) :
a[i] * (resl.rmx + b[i] + resr.lmx));
}
printf("%lld", ans);
return 0;
}