#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")
int n,h[100005],w[100005];
int num=0,l,r,lft,rht,cnt;
int node,exist[100005],a[100005];
long long pp=1e9*(1e9+7),sum=0,opt2=0,ans=0,val[100005];
int md=1e9+7;
int opt[100005],headl[1000005],headr[100005];
int value[100005];
vector<pair<int,int> > v;
long long supersum(int a,int b)
{
return ((long long)(a+b)*(long long)(b-a+1)/2)%(long long)md;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
headl[i]=i;
headr[i]=i;
scanf("%d",&h[i]);
v.push_back({-h[i],i});
}
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
value[i]=w[i];
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
//printf("%d %d\n",v[i].first,v[i].second);
if(i==0)
{
cnt=1;
}else
{
if(v[i].first!=v[i-1].first)++cnt;
}
a[cnt]=-v[i].first;
node=v[i].second;
l=value[node-1];
r=value[node+1];
lft=node;
rht=node;
opt2=w[node];
if(exist[node-1]!=0)
{
opt2+=l;
opt2%=md;
sum-=supersum(1,l);
sum+=pp;
sum%=md;
lft=headl[node-1];
}
if(exist[node+1]!=0)
{
opt2+=r;
opt2%=md;
sum-=supersum(1,r);
sum+=pp;
sum%=md;
rht=headr[node+1];
}
sum+=supersum(1,opt2);
sum%=md;
//printf("%lld\n",sum);
headr[lft]=rht;
headl[rht]=lft;
value[lft]=opt2;
value[rht]=opt2;
++exist[node];
val[cnt]=sum;
}
ans+=(supersum(1,a[cnt]))*val[cnt];
ans%=md;
for(int i=cnt-1;i>=1;i--)
{
ans-=val[i]*(supersum(a[i]+1,a[i+1]));
ans+=pp;
ans%=md;
}
printf("%lld\n",ans);
}
/*
5
2 7 4 1 4
3 2 4 2 4
*/
/*
3
1 3 2
2 3 1
*/
/*
5
2 5 1 5 3
2 2 3 2 3
*/
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
fancyfence.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
fancyfence.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%d",&h[i]);
| ~~~~~^~~~~~~~~~~~
fancyfence.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d",&w[i]);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
16 ms |
2532 KB |
Output is correct |
4 |
Correct |
38 ms |
4668 KB |
Output is correct |
5 |
Correct |
34 ms |
4740 KB |
Output is correct |
6 |
Correct |
29 ms |
4724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
3 |
Correct |
16 ms |
2888 KB |
Output is correct |
4 |
Correct |
37 ms |
5624 KB |
Output is correct |
5 |
Correct |
32 ms |
5564 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
992 KB |
Output is correct |
4 |
Correct |
22 ms |
2980 KB |
Output is correct |
5 |
Correct |
39 ms |
5508 KB |
Output is correct |
6 |
Correct |
31 ms |
5540 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
17 ms |
3244 KB |
Output is correct |
10 |
Correct |
31 ms |
6552 KB |
Output is correct |
11 |
Correct |
34 ms |
6576 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
16 ms |
2572 KB |
Output is correct |
12 |
Correct |
30 ms |
4660 KB |
Output is correct |
13 |
Correct |
33 ms |
4728 KB |
Output is correct |
14 |
Correct |
29 ms |
4672 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
980 KB |
Output is correct |
17 |
Correct |
20 ms |
3004 KB |
Output is correct |
18 |
Correct |
44 ms |
5444 KB |
Output is correct |
19 |
Correct |
34 ms |
5528 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
5 ms |
996 KB |
Output is correct |
22 |
Correct |
16 ms |
3140 KB |
Output is correct |
23 |
Correct |
30 ms |
6488 KB |
Output is correct |
24 |
Correct |
33 ms |
6584 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
1108 KB |
Output is correct |
31 |
Correct |
4 ms |
1108 KB |
Output is correct |
32 |
Correct |
18 ms |
2900 KB |
Output is correct |
33 |
Correct |
19 ms |
3572 KB |
Output is correct |
34 |
Correct |
35 ms |
6332 KB |
Output is correct |
35 |
Correct |
38 ms |
5308 KB |
Output is correct |
36 |
Correct |
39 ms |
6680 KB |
Output is correct |
37 |
Correct |
40 ms |
6172 KB |
Output is correct |
38 |
Correct |
1 ms |
320 KB |
Output is correct |
39 |
Correct |
49 ms |
6120 KB |
Output is correct |
40 |
Correct |
36 ms |
6588 KB |
Output is correct |
41 |
Correct |
34 ms |
6588 KB |
Output is correct |
42 |
Correct |
35 ms |
6620 KB |
Output is correct |