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;
#define mod (long long)1000000007
ll sum(ll n) {
return (n*(n+1))/2;
}
ll multiply(ll a, ll b) {
a%=mod;
b%=mod;
return (a*b)%mod;
}
int main () {
ll n, ans = 0;
cin >> n;
ll h[n+1] = {0}, w[n+1] = {0}, pref[n+1] = {0}, dp[n+1] = {0};
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++) {
pref[i] = pref[i-1]+w[i];
}
stack<ll> s;
s.push(0);
for(int i = 1;i<=n;i++) {
while(!s.empty() and h[s.top()] >= h[i])s.pop();
dp[i] = dp[s.top()]+multiply(sum(h[i]), (pref[i] - pref[s.top()]));
ans += multiply(dp[s.top()], w[i]) + multiply(sum(h[i]), multiply(pref[i-1] - pref[s.top()], w[i]))+multiply(sum(h[i]), sum(w[i]));
dp[i]%=mod;
ans%=mod;s.push(i);
}
cout<<ans;
}
# | 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... |