//1L1YA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define Pb push_back
#define dokme(x) cout << x << endl, exit(0)
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define sep ' '
#define all(x) x.begin(),x.end()
#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lc id<<1
#define rc lc|1
const ll mod=1e9+7;
const ll oo=4e18;
const int N=1e5+5;
const int lg=23;
ll n,h[N],w[N],dp[N],lst[N],sum[N];
ll C(ll x){
return (x*(x-1)/2)%mod;
}
int main(){
FastIO
cin >> n;
for(int i=1;i<=n;i++)
cin >> h[i];
for(int i=1;i<=n;i++)
cin >> w[i],sum[i]=(sum[i-1]+w[i])%mod;
vector<int> st;
st.Pb(0);
for(int i=1;i<=n;i++){
while(h[i]<=h[st.back()])
st.pop_back();
lst[i]=st.back();
st.Pb(i);
}
ll ans=0;
for(int i=1;i<=n;i++){
ll W=(sum[i]-sum[lst[i]]+mod)%mod;
ll p=C(W+1);
p=(p-C((W-w[i]+1+mod)%mod)+mod)%mod;
ans+=p*C(h[i]+1)%mod;
ans+=dp[lst[i]]*w[i]%mod;
dp[i]=(W*C(h[i]+1)%mod+dp[lst[i]])%mod;
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |