This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |