#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(),v.end()
#define pb push_back
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
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 |
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 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2488 KB |
Output is correct |
5 |
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 |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
42 ms |
6608 KB |
Output is correct |
4 |
Correct |
81 ms |
11004 KB |
Output is correct |
5 |
Correct |
85 ms |
11056 KB |
Output is correct |
6 |
Correct |
86 ms |
10956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
10 ms |
3480 KB |
Output is correct |
3 |
Correct |
47 ms |
7000 KB |
Output is correct |
4 |
Correct |
105 ms |
12200 KB |
Output is correct |
5 |
Correct |
99 ms |
11776 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
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 |
9 ms |
3396 KB |
Output is correct |
4 |
Correct |
50 ms |
6980 KB |
Output is correct |
5 |
Correct |
93 ms |
11772 KB |
Output is correct |
6 |
Correct |
100 ms |
11776 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
9 ms |
3332 KB |
Output is correct |
9 |
Correct |
49 ms |
7040 KB |
Output is correct |
10 |
Correct |
93 ms |
11512 KB |
Output is correct |
11 |
Correct |
97 ms |
11876 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 |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 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 |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 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 |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2520 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 |
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 |
40 ms |
6684 KB |
Output is correct |
12 |
Correct |
75 ms |
10756 KB |
Output is correct |
13 |
Correct |
103 ms |
10952 KB |
Output is correct |
14 |
Correct |
77 ms |
11016 KB |
Output is correct |
15 |
Correct |
1 ms |
2588 KB |
Output is correct |
16 |
Correct |
9 ms |
3480 KB |
Output is correct |
17 |
Correct |
47 ms |
6920 KB |
Output is correct |
18 |
Correct |
93 ms |
11780 KB |
Output is correct |
19 |
Correct |
95 ms |
11908 KB |
Output is correct |
20 |
Correct |
2 ms |
2392 KB |
Output is correct |
21 |
Correct |
9 ms |
3480 KB |
Output is correct |
22 |
Correct |
51 ms |
7176 KB |
Output is correct |
23 |
Correct |
105 ms |
11764 KB |
Output is correct |
24 |
Correct |
92 ms |
11764 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 |
3 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
12 ms |
3480 KB |
Output is correct |
31 |
Correct |
12 ms |
3476 KB |
Output is correct |
32 |
Correct |
49 ms |
6920 KB |
Output is correct |
33 |
Correct |
65 ms |
6920 KB |
Output is correct |
34 |
Correct |
116 ms |
11376 KB |
Output is correct |
35 |
Correct |
96 ms |
12036 KB |
Output is correct |
36 |
Correct |
124 ms |
12188 KB |
Output is correct |
37 |
Correct |
116 ms |
11780 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
122 ms |
11544 KB |
Output is correct |
40 |
Correct |
126 ms |
12292 KB |
Output is correct |
41 |
Correct |
122 ms |
11768 KB |
Output is correct |
42 |
Correct |
105 ms |
11776 KB |
Output is correct |