#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
const int mod=1e9+7;
int cal(int left,int right,int up,int down){
int width=right-left+1;
width%=mod;
int ans=width*(width-1)/2;
ans%=mod;
int tmp=up*(up+1)-down*(down+1);
tmp/=2;
tmp%=mod;
//cout<<left<<' '<<right<<' '<<up<<' '<<down<<' '<<ans*tmp<<endl;
return ans*tmp%mod;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<int> a(n+1),b(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
b[i]+=b[i-1];
}
int ans=0;
vector<int> v;
v.push_back(0);
for(int i=1;i<=n;i++){
while(a[i]<=a[v.back()]){
//cout<<"OK"<<endl;
int tmp=v.back();
v.pop_back();
ans+=cal(b[v.back()],b[i-1],a[tmp],max(a[i],a[v.back()]));
ans%=mod;
}
v.push_back(i);
}
while(v.size()>1){
int tmp=v.back();
v.pop_back();
ans+=cal(b[v.back()],b[n],a[tmp],a[v.back()]);
ans%=mod;
}
cout<<ans<<endl;
return 0;
}
/*
4
1 3 2 4
1 1 1 1
32
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
284 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
12 ms |
1600 KB |
Output is correct |
4 |
Correct |
22 ms |
3028 KB |
Output is correct |
5 |
Correct |
16 ms |
3012 KB |
Output is correct |
6 |
Correct |
19 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
10 ms |
1124 KB |
Output is correct |
4 |
Correct |
19 ms |
1896 KB |
Output is correct |
5 |
Correct |
22 ms |
1900 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
1108 KB |
Output is correct |
5 |
Correct |
23 ms |
1900 KB |
Output is correct |
6 |
Correct |
29 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
11 ms |
1500 KB |
Output is correct |
10 |
Correct |
20 ms |
3048 KB |
Output is correct |
11 |
Correct |
22 ms |
3432 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 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 |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
9 ms |
1680 KB |
Output is correct |
12 |
Correct |
16 ms |
3032 KB |
Output is correct |
13 |
Correct |
16 ms |
3036 KB |
Output is correct |
14 |
Correct |
16 ms |
3064 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
11 ms |
1996 KB |
Output is correct |
18 |
Correct |
21 ms |
3784 KB |
Output is correct |
19 |
Correct |
24 ms |
3928 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
12 ms |
2392 KB |
Output is correct |
23 |
Correct |
21 ms |
4808 KB |
Output is correct |
24 |
Correct |
27 ms |
4956 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
14 ms |
1872 KB |
Output is correct |
33 |
Correct |
11 ms |
2004 KB |
Output is correct |
34 |
Correct |
20 ms |
3548 KB |
Output is correct |
35 |
Correct |
20 ms |
3540 KB |
Output is correct |
36 |
Correct |
21 ms |
3788 KB |
Output is correct |
37 |
Correct |
21 ms |
3784 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
22 ms |
3784 KB |
Output is correct |
40 |
Correct |
22 ms |
3804 KB |
Output is correct |
41 |
Correct |
23 ms |
3936 KB |
Output is correct |
42 |
Correct |
22 ms |
4428 KB |
Output is correct |