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;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int MOD = 1e9+7;
ll h[N], w[N], pw[N], dp[N], n;
void solve() {
cin>>n;
for (int i=1; i<=n; ++i) cin>>h[i];
for (int i=1; i<=n; ++i) cin>>w[i];
for (int i=1; i<=n; ++i) pw[i]=pw[i-1]+w[i];
dp[0]=0, h[0]=0;
ll ans=0; // dp[i] -> from {1, 2, ..., i} ends on rightmost point of i
stack<int> st; st.push(0);
for (int i=1; i<=n; ++i) {
while (h[st.top()] > h[i]) st.pop();
(ans+=dp[st.top()]*w[i])%=MOD;
ll hl=(h[i]*(h[i]+1)/2)%MOD, wl=(w[i]*(w[i]+1)/2)%MOD;
(dp[i]+=dp[st.top()]+(pw[i]-pw[st.top()])*hl)%=MOD;
(ans+=((w[i]*(pw[i-1]-pw[st.top()]))%MOD)*hl)%=MOD;
(ans+=wl*hl)%=MOD;
st.push(i);
}
/*
cout<<"dp: ";
for (int i=1; i<=n; ++i) cout<<dp[i]<<" ";
cout<<"\n";
*/
assert(ans >= 0 && ans < MOD);
cout<<ans<<"\n";
}
int main() {
int t=1; //cin>>t;
while (t--) solve();
}
# | 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... |