#include <bits/stdc++.h>
using namespace std;
// * No One Dies a Virgin, Life Fucks Us All
typedef long long ll;
#define nl '\n'
#define ff first
#define ss second
#define pb push_back
#define sik(x) {cout << x << nl; return;}
constexpr ll maxn = 1e5+5, mod = 1e9 + 7, inf = 1e17, SQ = 450, LG = 20;
typedef pair<int, int> pii;
int n, w[maxn], h[maxn], l[maxn], r[maxn];
ll ps[maxn], ans;
inline ll C2(ll x) {
return (x * (x - 1) / 2) % mod;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1 ; i <= n ; i ++) cin >> h[i];
for (int i = 1 ; i <= n ; i ++) cin >> w[i];
for (int i = 1 ; i <= n ; i ++) ps[i] = ps[i - 1] + w[i];
vector<int> stk;
for (int i = 1 ; i <= n ; i ++) {
while (stk.size() && h[stk.back()] > h[i]) stk.pop_back();
l[i] = stk.size() ? stk.back() : 0;
stk.pb(i);
}
stk.clear();
for (int i = n ; i >= 1 ; i --) {
while (stk.size() && h[stk.back()] >= h[i]) stk.pop_back();
r[i] = stk.size() ? stk.back() : n+1;
stk.pb(i);
}
ll ans = 0;
for (int i = 1 ; i <= n ; i ++) {
ll lf = (ps[i - 1] - ps[l[i]] + mod) % mod, rt = (ps[r[i] - 1] - ps[i] + mod) % mod;
ll H = C2(h[i] + 1);
ll W = lf * (w[i] + rt) % mod + w[i] * rt % mod + C2(w[i] + 1);
while (W >= mod) W -= mod;
ans = (ans + H * W) % mod;
}
cout << ans << nl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |