#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int div2 = 500000004;
const int N = 1e5 + 7;
int n, h[N], w[N], lef[N], rig[N], ps[N];
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
if(x < 0) x += mod;
return x;
}
int mul(int x, int y) {
return (1ll * x * y) % mod;
}
int get(int l, int r) {
return add(ps[r], -ps[l - 1]);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i];
for(int i = 1; i <= n; i++) cin >> w[i], ps[i] = add(ps[i - 1], w[i]);
ps[n + 1] = ps[n];
vector<int> st;
h[0] = h[n + 1] = -inf;
st.pb(0);
for(int i = 1; i <= n; i++) {
while(h[st.back()] >= h[i]) st.pop_back();
lef[i] = st.back();
st.pb(i);
}
st.clear();
st.pb(n + 1);
int ans = 0;
for(int i = n; i > 0; i--) {
while(h[st.back()] > h[i]) st.pop_back();
rig[i] = st.back();
st.pb(i);
int tot = get(lef[i] + 1, rig[i] - 1);
tot = mul(mul(tot, tot + 1), div2);
int l = get(lef[i] + 1, i - 1);
int r = get(i + 1, rig[i] - 1);
l = mul(mul(l, l + 1), div2);
r = mul(mul(r, r + 1), div2);
int ext2 = mul(mul(h[i], h[i] + 1), div2);
tot = add(tot, -l);
tot = add(tot, -r);
ans = add(ans, mul(ext2, tot));
}
cout << ans << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |