#include <bits/stdc++.h>
using namespace std;
#define int long long
#define h first
#define w second
pair<int,int>a[100005];
int f[100005];
int dp[100005];
const int mod=1e9+7;
int n;
int mul(int a,int b)
{
return a*b%mod;
}
int sum(int a)
{
return (a*(a+1)/2)%mod;
}
main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].h;
for(int i=1;i<=n;i++){
cin>>a[i].w;
f[i]=(f[i-1]+a[i].w)%mod;
}
int ans=0;
stack<int>s;
s.push(0);
for(int i=1;i<=n;i++){
while(s.size()&&a[s.top()].h>=a[i].h)s.pop();
int len=(f[i]-f[s.top()]+mod)%mod;
int l1=(len-a[i].w+mod)%mod;
int A=0,B=0;
(A+=mul(sum(a[i].h),sum(a[i].w)))%=mod;
(A+=(mul(mul(l1,sum(a[i].h)),a[i].w)))%=mod;
(B+=mul(dp[s.top()],a[i].w))%=mod;
dp[i]=(dp[s.top()]+mul(len,sum(a[i].h)))%mod;
ans=(ans+A+B)%mod;
s.push(i);
}
cout<<ans;
return 0;
}
Compilation message
fancyfence.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
18 ms |
2464 KB |
Output is correct |
4 |
Correct |
32 ms |
4516 KB |
Output is correct |
5 |
Correct |
33 ms |
4692 KB |
Output is correct |
6 |
Correct |
36 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
860 KB |
Output is correct |
3 |
Correct |
25 ms |
2772 KB |
Output is correct |
4 |
Correct |
49 ms |
5340 KB |
Output is correct |
5 |
Correct |
51 ms |
5456 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
6 ms |
860 KB |
Output is correct |
4 |
Correct |
22 ms |
2848 KB |
Output is correct |
5 |
Correct |
50 ms |
5460 KB |
Output is correct |
6 |
Correct |
47 ms |
5456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
836 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
56 ms |
5980 KB |
Output is correct |
11 |
Correct |
50 ms |
6224 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
448 KB |
Output is correct |
7 |
Correct |
0 ms |
448 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
16 ms |
2396 KB |
Output is correct |
12 |
Correct |
32 ms |
4692 KB |
Output is correct |
13 |
Correct |
42 ms |
4688 KB |
Output is correct |
14 |
Correct |
34 ms |
4696 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
17 |
Correct |
24 ms |
2952 KB |
Output is correct |
18 |
Correct |
43 ms |
5460 KB |
Output is correct |
19 |
Correct |
46 ms |
5468 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
860 KB |
Output is correct |
22 |
Correct |
25 ms |
3116 KB |
Output is correct |
23 |
Correct |
46 ms |
6120 KB |
Output is correct |
24 |
Correct |
45 ms |
6224 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
880 KB |
Output is correct |
31 |
Correct |
4 ms |
848 KB |
Output is correct |
32 |
Correct |
23 ms |
2880 KB |
Output is correct |
33 |
Correct |
24 ms |
2920 KB |
Output is correct |
34 |
Correct |
49 ms |
5284 KB |
Output is correct |
35 |
Correct |
54 ms |
5132 KB |
Output is correct |
36 |
Correct |
61 ms |
5456 KB |
Output is correct |
37 |
Correct |
46 ms |
5340 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
47 ms |
5460 KB |
Output is correct |
40 |
Correct |
46 ms |
5456 KB |
Output is correct |
41 |
Correct |
46 ms |
5460 KB |
Output is correct |
42 |
Correct |
47 ms |
5712 KB |
Output is correct |