#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+3;
const int mod = 1e9+7;
ll i,j,p,q,n,m,k,h[maxn],w[maxn],l[maxn],r[maxn],pref[maxn],ans,same[maxn];
stack <ll> s;
bool marked[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(i=1;i<=n;i++)
{
l[i] = 1;
r[i] = n;
cin>>h[i];
}
for(i=1;i<=n;i++)
{
cin>>w[i];
pref[i] = pref[i-1]+w[i];
}
for(i=1;i<=n;i++)
{
while(!s.empty() && h[i]<h[s.top()])
s.pop();
if(!s.empty())
l[i] = s.top()+1;
s.push(i);
}
while(!s.empty())
s.pop();
for(i=n;i>=1;i--)
{
while(!s.empty() && h[i]<=h[s.top()])
s.pop();
if(!s.empty())
r[i] = s.top()-1;
s.push(i);
}
for(i=1;i<=n;i++)
{
q = pref[r[i]] - pref[i-1];///wmain
q%=mod;
p = pref[i-1]-pref[l[i]-1];///wside - left
p%=mod;
k = (h[i]*1ll*(h[i]+1))/2;k%=mod;
ans+=(((p*q)%mod)*k)%mod;ans%=mod;
///ne sa vsichki prebroeni!!!
q = pref[r[i]] - pref[i];q%=mod;
p = w[i];
ans+=(((p*q)%mod)*k)%mod;ans%=mod;
}
for(i=1;i<=n;i++)
{
k = (h[i]*1ll*(h[i]+1))/2;k%=mod;
p = (w[i]*1ll*(w[i]+1))/2;p%=mod;
ans+=(p*k)%mod;ans%=mod;
}
j = pref[n];j%=mod;
k = (h[1]*1ll*(h[1]+1))/2;k%=mod;
p = (j*(j+1))/2;p%=mod;
q = 0;
q=(p*k)%mod;ans%=mod;
//cout<<q<<endl;
cout<<ans<<endl;
return 0;
}
/**
5
3 2 4 1 5
1 1 1 1 1
2
2 2
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
9 ms |
5348 KB |
Output is correct |
4 |
Correct |
17 ms |
6760 KB |
Output is correct |
5 |
Correct |
16 ms |
6368 KB |
Output is correct |
6 |
Correct |
15 ms |
6216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
10 ms |
4956 KB |
Output is correct |
4 |
Correct |
18 ms |
5724 KB |
Output is correct |
5 |
Correct |
19 ms |
5852 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4624 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4700 KB |
Output is correct |
4 |
Correct |
10 ms |
5980 KB |
Output is correct |
5 |
Correct |
19 ms |
7768 KB |
Output is correct |
6 |
Correct |
19 ms |
7868 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
10 ms |
5968 KB |
Output is correct |
10 |
Correct |
18 ms |
7512 KB |
Output is correct |
11 |
Correct |
19 ms |
7760 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4568 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4640 KB |
Output is correct |
9 |
Correct |
1 ms |
4592 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
9 ms |
5212 KB |
Output is correct |
12 |
Correct |
16 ms |
6748 KB |
Output is correct |
13 |
Correct |
17 ms |
6488 KB |
Output is correct |
14 |
Correct |
15 ms |
6216 KB |
Output is correct |
15 |
Correct |
1 ms |
4592 KB |
Output is correct |
16 |
Correct |
3 ms |
4768 KB |
Output is correct |
17 |
Correct |
12 ms |
5980 KB |
Output is correct |
18 |
Correct |
19 ms |
7760 KB |
Output is correct |
19 |
Correct |
19 ms |
7860 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
3 ms |
4696 KB |
Output is correct |
22 |
Correct |
10 ms |
6232 KB |
Output is correct |
23 |
Correct |
19 ms |
7492 KB |
Output is correct |
24 |
Correct |
21 ms |
7748 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4596 KB |
Output is correct |
29 |
Correct |
1 ms |
4576 KB |
Output is correct |
30 |
Correct |
3 ms |
4700 KB |
Output is correct |
31 |
Correct |
3 ms |
4700 KB |
Output is correct |
32 |
Correct |
10 ms |
5468 KB |
Output is correct |
33 |
Correct |
11 ms |
5720 KB |
Output is correct |
34 |
Correct |
20 ms |
6748 KB |
Output is correct |
35 |
Correct |
19 ms |
6748 KB |
Output is correct |
36 |
Correct |
20 ms |
6748 KB |
Output is correct |
37 |
Correct |
20 ms |
7004 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
19 ms |
6996 KB |
Output is correct |
40 |
Correct |
20 ms |
7000 KB |
Output is correct |
41 |
Correct |
24 ms |
7012 KB |
Output is correct |
42 |
Correct |
20 ms |
7260 KB |
Output is correct |