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 f first
#define s second
#define ll long long
const ll mod=1e9+7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vector<ll> h(n+1,0), w(n+1,0);
for(int i=0;i<n;++i) cin>>h[i];
for(int i=0;i<n;++i) cin>>w[i];
n++;
vector<pair<ll,ll>> t;
t.push_back({0,0});
ll ans=0;
for(int i=0;i<n;++i) {
ll sum=0;
while(t.back().f>h[i]) {
ll mn=max(h[i],t[t.size()-2].f);
ll mx=t.back().f-mn;
sum=(sum+t.back().s)%mod;
t.pop_back();
ans+=(mx*(mx+1)%mod*500000004%mod+mx*mn%mod)*(sum*(sum+1)%mod*500000004%mod)%mod;
ans%=mod;
}
if(t.back().f==h[i]) t.back().s=(t.back().s+sum+w[i])%mod;
else t.push_back({h[i], (sum+w[i])%mod});
}
cout<<ans<<endl;
return 0;
}
# | 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... |