#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;
stack <ll> s;
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;
}
cout<<ans<<endl;
return 0;
}
/**
5
3 2 4 1 5
1 1 1 1 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
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 |
2392 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 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2520 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |