#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(),v.end()
#define pb push_back
const int mod=1e9+7;
int f(int a){
a%=mod;
return ((a%mod*(a+1)%mod)%mod*500000004%mod)%mod;
}
const int mxn=1e5+100;
int sz[mxn];
int W[mxn];
int p[mxn];
int find(int node){
return node==p[node]?node:p[node]=find(p[node]);
}
void merge(int a,int b){
a=find(a);
b=find(b);
if(a==b) return;
if(sz[a]<sz[b]) swap(a,b);
sz[a]+=sz[b];
p[b]=a;
W[a]=(W[a]%mod+W[b]%mod)%mod;
}
signed main(){
int n;
cin>>n;
vector<int> h(n+1);
vector<int> w(n+1);
for(int i=0;i<n;i++){
cin>>h[i];
}
for(int i=0;i<n;i++){
cin>>w[i];
}
int ans=0;
for(int i=0;i<n;i++){
ans=(ans%mod+f(h[i])%mod*f(w[i])%mod)%mod;
}
ans%=mod;
vector<vector<int>> v;
for(int i=0;i+1<n;i++){
v.pb({i,i+1,min(h[i],h[i+1])});
}
sort(all(v),[&](auto a,auto b){
return a[2]>b[2];
});
for(int i=0;i<n;i++){
sz[i]=1;
p[i]=i;
W[i]+=w[i];
}
for(auto x:v){
int a=W[find(x[0])];
int b=W[find(x[1])];
a+=mod;
b+=mod;
a%=mod;
b%=mod;
merge(x[0],x[1]);
ans=(ans%mod+a%mod*b%mod*f(x[2])%mod)%mod;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
33 ms |
6032 KB |
Output is correct |
4 |
Correct |
71 ms |
10240 KB |
Output is correct |
5 |
Correct |
77 ms |
9724 KB |
Output is correct |
6 |
Correct |
70 ms |
10436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
9 ms |
3232 KB |
Output is correct |
3 |
Correct |
44 ms |
6012 KB |
Output is correct |
4 |
Correct |
111 ms |
9708 KB |
Output is correct |
5 |
Correct |
91 ms |
9732 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
9 ms |
3216 KB |
Output is correct |
4 |
Correct |
40 ms |
6024 KB |
Output is correct |
5 |
Correct |
83 ms |
9612 KB |
Output is correct |
6 |
Correct |
93 ms |
9732 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
9 ms |
3212 KB |
Output is correct |
9 |
Correct |
46 ms |
6152 KB |
Output is correct |
10 |
Correct |
92 ms |
9604 KB |
Output is correct |
11 |
Correct |
86 ms |
9732 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2420 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
38 ms |
6024 KB |
Output is correct |
12 |
Correct |
71 ms |
10236 KB |
Output is correct |
13 |
Correct |
73 ms |
9728 KB |
Output is correct |
14 |
Correct |
81 ms |
9604 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
9 ms |
3220 KB |
Output is correct |
17 |
Correct |
50 ms |
6152 KB |
Output is correct |
18 |
Correct |
90 ms |
9732 KB |
Output is correct |
19 |
Correct |
97 ms |
9612 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
8 ms |
3232 KB |
Output is correct |
22 |
Correct |
41 ms |
6024 KB |
Output is correct |
23 |
Correct |
89 ms |
9740 KB |
Output is correct |
24 |
Correct |
84 ms |
9728 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
12 ms |
3224 KB |
Output is correct |
31 |
Correct |
10 ms |
3224 KB |
Output is correct |
32 |
Correct |
46 ms |
6016 KB |
Output is correct |
33 |
Correct |
51 ms |
6148 KB |
Output is correct |
34 |
Correct |
101 ms |
10516 KB |
Output is correct |
35 |
Correct |
101 ms |
9600 KB |
Output is correct |
36 |
Correct |
101 ms |
9724 KB |
Output is correct |
37 |
Correct |
135 ms |
9728 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
117 ms |
9732 KB |
Output is correct |
40 |
Correct |
106 ms |
9724 KB |
Output is correct |
41 |
Correct |
99 ms |
9732 KB |
Output is correct |
42 |
Correct |
102 ms |
9872 KB |
Output is correct |