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
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int h[N],w[N],pre[N],l[N],r[N];
void add(int &a,int b) {
    a += b;
    while (a >= mod) a -= mod;
}
void sub(int &a,int b) {
    a -= b;
    while (a < 0) a += mod;
}
int sum(int l,int r) {
    return (pre[r] - pre[l - 1] + mod) % mod;
}
int calc(int a,int b) {
    int ans = (a * (a + 1) / 2) % mod;
    ans = (ans * ((b * (b + 1) / 2) % mod)) % mod;
    return ans;
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> h[i];
    pre[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
        pre[i] = (pre[i - 1] + w[i]) % mod;
    }
    stack<int>st;
    st.push(0);
    for (int i = 1; i <= n; i++) {
        while (!st.empty() && h[st.top()] > h[i]) st.pop();
        l[i] = st.top() + 1;
        st.push(i);
    }
    while (!st.empty()) st.pop();
    st.push(n + 1);
    for (int i = n; i > 0; i--) {
        while (!st.empty() && h[st.top()] > h[i]) st.pop();
        r[i] = st.top() - 1;
        st.push(i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        add(ans,calc(h[i],sum(l[i],r[i])));
        sub(ans,calc(h[i],sum(l[i],i - 1)));
        sub(ans,calc(h[i],sum(i + 1,r[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... |