#include<bits/stdc++.h>
const int MOD = int(1e9) + 7;
const int64_t INV_2 = 500000004;
int f(int a, int b){
a = a % MOD;
b = b % MOD;
return int64_t(a) * (a + 1) % MOD * INV_2 % MOD * int64_t(b) * (b + 1) % MOD * INV_2 % MOD;
}
int main(){
using namespace std;
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vector<int> h(n), w(n);
int all_1 = 1;
for(int i = 0;i < n;i++) cin >> h[i];
for(int i = 0;i < n;i++) {
cin >> w[i];
all_1 &= (w[i] == 1);
}
int ans = 0;
for(int i = 0;i < n;i++){
int l = i, r = i;
int64_t sL = 0;
int64_t sR = 0;
while(l - 1 >= 0 && h[l - 1] >= h[i]){
l -= 1;
sL += w[l];
}
while(r + 1 < n && h[r + 1] > h[i]){
r += 1;
sR += w[r];
}
// but it must strictly contain me
sL %= MOD;
sR %= MOD;
if(w[i] == 1)
ans += f(h[i], w[i]) * int64_t(sL+1) % MOD * int64_t(sR+1) % MOD;
else {
ans += h[i] * 1ll * (h[i] + 1) % MOD * int64_t(sL+1) % MOD * int64_t(sR+1) % MOD;
if(ans >= MOD) ans -= MOD;
if(w[i] > 2) ans += f(h[i], w[i] - 2);
if(ans >= MOD) ans -= MOD;
}
}
cout << ans << '\n';
return 0;
}
# | 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... |