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 int ll
typedef pair<int,int> pi;
const int MM = 1e5+5,MOD = 1e9+7,inv2 = 500000004;
int n,h[MM],w[MM],psa[MM],dp[MM];
int c2(int x){
int val = (1ll*x*(x+1) % MOD) * inv2 % MOD;
return val;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i];
for(int i = 1; i <= n; i++) cin >> w[i],psa[i] = (psa[i-1] + w[i]) % MOD;
stack<int> st; st.push(0);
int ans = 0;
for(int i = 1; i <= n; i++){
while(st.size() && h[st.top()] >= h[i]) st.pop();
int j = st.top();
dp[i] = dp[j];
dp[i] = (dp[i] + 1ll*w[j]*c2(h[j]) % MOD)%MOD;
dp[i] = (dp[i] + (1ll*((psa[i-1] - psa[j]+MOD)%MOD)*c2(h[i])) % MOD) % MOD;
ans = ((ans + 1ll*dp[i]*w[i] % MOD)%MOD + 1ll*c2(w[i])*c2(h[i]) % MOD);
st.push(i);
}
cout << ans << "\n";
}
# | 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... |