#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
ll dp[100001];
const ll linf = 1e18 + 1;
const ll mod = 1e9 + 7;
int main(){
int n; cin >> n;
ll 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];
}
ll pref[n + 1];
pref[0] = 0;
for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + w[i];
stack <int> st;
st.push(1);
int l[n + 1], r[n + 1];
l[1] = 0;
for(int i = 2; i <= n; i++){
while(!st.empty()){
int v = st.top();
if(h[v] <= h[i]) break;
st.pop();
}
if(st.empty()) l[i] = 0;
else l[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
r[n] = n + 1;
st.push(n);
for(int i= n - 1; i >= 1; i--){
while(!st.empty()){
int v = st.top();
if(h[v] < h[i]) break;
st.pop();
}
if(st.empty()) r[i] = n + 1;
else r[i] = st.top();
st.push(i);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ll x = h[i] * (h[i] - 1) / 2 + h[i]; x %= mod;
int L = l[i], R = r[i];
ll y = pref[R - 1] - pref[L];
y = y * (y - 1) / 2 + y; y %= mod;
ans += x * y;
ans %= mod;
y = pref[i - 1] - pref[L];
y = y * (y - 1) / 2 + y; y %= mod;
ans -= x * y; ans %= mod; ans += mod; ans %= mod;
y = pref[R - 1] - pref[i];
y = y * (y - 1) / 2 + y; y %= mod;
ans -= x * y; ans %= mod; ans += mod; ans %= mod;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
304 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |