#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll,ll>;
using vi = vector<ll>;
using ml = map<ll,ll>;
const ll mod=1000000007;
#define pb push_back
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define newl '\n'
#define form(m,it) for(auto it=m.begin();it!=m.end(); it++)
#define ff first
#define ss second
ll nc2(ll a){
ll b=a+1;
if(a%2==0){
a/=2;
} else {
b/=2;
}
return (a*b)%mod;
}
void solve(){
ll n; cin>>n;
ll h[n+1],w[n+1];
forp(i,1,n){cin>>h[i];}
forp(i,1,n){cin>>w[i];}
ll dp[n+1],m[n+1],pre[n+1];
stack<pl> st;
st.push({0,0});
dp[0]=pre[0]=h[0]=w[0]=0;
forp(i,1,n){
pre[i]=pre[i-1]+w[i]; pre[i]%=mod;
while(st.top().ff>h[i]){
st.pop();
}
m[i]=st.top().second;
st.push({h[i],i});
}
ll ans=0,j,c,d;
forp(i,1,n){
j=m[i];
dp[i]=dp[j];
c=nc2(h[i]); c+=mod; c%=mod; c*=(pre[i]-pre[j]); c+=mod; c%=mod;
dp[i]+=c; dp[i]+=mod; dp[i]%=mod;
c=nc2(h[i]); c+=mod; c%=mod; c*=nc2(w[i]-1); c+=mod; c%=mod;
d=nc2(h[i]); d+=mod; d%=mod; d*=(w[i]-1); d+=mod; d%=mod; d*=(pre[i-1]-pre[j]); d+=mod; d%=mod;
c+=d; c+=mod; c%=mod;
ans+=c; ans+=mod; ans%=mod;
ans+=(dp[j]*(w[i]-1)); ans+=mod; ans%=mod;
ans+=dp[i]; ans+=mod; ans%=mod;
}
cout<<(ans+mod)%mod<<newl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
13 ms |
2564 KB |
Output is correct |
4 |
Correct |
29 ms |
5576 KB |
Output is correct |
5 |
Correct |
23 ms |
4816 KB |
Output is correct |
6 |
Correct |
21 ms |
4304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
13 ms |
3020 KB |
Output is correct |
4 |
Correct |
31 ms |
5836 KB |
Output is correct |
5 |
Correct |
31 ms |
5840 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
976 KB |
Output is correct |
4 |
Correct |
18 ms |
4048 KB |
Output is correct |
5 |
Correct |
27 ms |
7736 KB |
Output is correct |
6 |
Correct |
34 ms |
7868 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
3 ms |
976 KB |
Output is correct |
9 |
Correct |
17 ms |
4048 KB |
Output is correct |
10 |
Correct |
30 ms |
7652 KB |
Output is correct |
11 |
Correct |
28 ms |
7748 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
0 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
14 ms |
3084 KB |
Output is correct |
12 |
Correct |
22 ms |
6600 KB |
Output is correct |
13 |
Correct |
25 ms |
5936 KB |
Output is correct |
14 |
Correct |
23 ms |
5448 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
4 ms |
964 KB |
Output is correct |
17 |
Correct |
14 ms |
4024 KB |
Output is correct |
18 |
Correct |
30 ms |
7732 KB |
Output is correct |
19 |
Correct |
35 ms |
7876 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
3 ms |
976 KB |
Output is correct |
22 |
Correct |
15 ms |
4024 KB |
Output is correct |
23 |
Correct |
32 ms |
7616 KB |
Output is correct |
24 |
Correct |
34 ms |
7780 KB |
Output is correct |
25 |
Correct |
0 ms |
208 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
3 ms |
848 KB |
Output is correct |
31 |
Correct |
3 ms |
848 KB |
Output is correct |
32 |
Correct |
16 ms |
3128 KB |
Output is correct |
33 |
Correct |
16 ms |
3152 KB |
Output is correct |
34 |
Correct |
26 ms |
5960 KB |
Output is correct |
35 |
Correct |
35 ms |
5940 KB |
Output is correct |
36 |
Correct |
37 ms |
6084 KB |
Output is correct |
37 |
Correct |
30 ms |
6088 KB |
Output is correct |
38 |
Correct |
1 ms |
208 KB |
Output is correct |
39 |
Correct |
27 ms |
6176 KB |
Output is correct |
40 |
Correct |
31 ms |
6132 KB |
Output is correct |
41 |
Correct |
30 ms |
6208 KB |
Output is correct |
42 |
Correct |
30 ms |
6972 KB |
Output is correct |