#include <iostream>
#include <vector>
using namespace std;
typedef long long llong;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int INF = 1e9 + 10;
int n;
int w[MAXN];
int h[MAXN];
llong dp[MAXN];
llong prefw[MAXN];
int toleft[MAXN];
void read()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> h[i];
}
for(int i = 1; i <= n; i++)
{
cin >> w[i];
}
}
llong comb(llong x)
{
if(x % 2 == 0)
{
llong x2 = x / 2;
return x2 * (x - 1LL) % MOD;
}
llong x2 = (x - 1) / 2;
return x * x2 % MOD;
}
void solve()
{
vector < pair < int, int > > st;
st.push_back({0, 0});
llong ans = 0;
for(int i = 1; i <= n + 1; i++)
{
llong sumw = 0;
while(st.back().first > h[i])
{
sumw += st.back().second;
llong hp = max(h[i], st[st.size() - 2].first);
llong deltah = (st.back().first - hp + MOD) % MOD;
ans += comb(sumw + 1) * ((comb(deltah + 1) + hp * deltah % MOD) % MOD) % MOD;
st.pop_back();
}
sumw = (sumw + w[i]) % MOD;
if(st.back().first == h[i])
{
st.back().second = (st.back().second + sumw) % MOD;
}
else
{
st.push_back({h[i], sumw});
}
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
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... |