#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int inf = 2e18,N = 5e5+1,MOD = 1e9+7;
int add(int x,int y) {
return ((x+y >= MOD) ? (x+y-MOD) : x+y);
}
int mult(int x,int y) {
return (x*y)%MOD;
}
void solve() {
int n;
cin >> n;
vi h(n+1),w(n+1);
for (int i=1;i<=n;i++) cin >> h[i];
for (int i=1;i<=n;i++) cin >> w[i];
vi L(n+1),R(n+1);
stack<int> stk;
for (int i=1;i<=n;i++) {
while (!stk.empty() && h[stk.top()] >= h[i]) stk.pop();
if (stk.empty()) L[i] = 1;
else L[i] = stk.top()+1;
stk.push(i);
}
while (!stk.empty()) stk.pop();
for (int i = n;i>=1;i--) {
while (!stk.empty() && h[stk.top()] > h[i]) stk.pop();
if (stk.empty()) R[i] = n;
else R[i] = stk.top()-1;
stk.push(i);
}
vi pref(n+1,0);
for (int i=1;i<=n;i++) pref[i] = pref[i-1]+w[i];
int ans = 0,anss = 0;
for (int i=1;i<=n;i++) {
ans = 0;
int tot = add(pref[R[i]],MOD-pref[L[i]-1]);
ans = add(ans,(tot*(tot+1)/2)%MOD);
int left = add(pref[i-1],MOD-(pref[L[i]-1])%MOD);
ans = add(ans,MOD-(left*(left+1)/2)%MOD);
int right = add(pref[R[i]],MOD-(pref[i])%MOD);
ans = add(ans,MOD-(right*(right+1)/2)%MOD);
ans = mult(ans,(h[i]*(h[i]+1)/2)%MOD);
anss = add(anss,ans);
}
cout << anss << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | 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... |