#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int add(int x, int y){
return (x + y < MOD ? x + y : x + y - MOD);
}
int sub(int x, int y){
return add(x - y, MOD);
}
int mul(int x, int y){
return (1ll * x * y) % MOD;
}
void solve()
{
int n; cin >> n;
vector<int> h(n + 1), w(n + 1);
FOR(i, 1, n) cin >> h[i];
FOR(i, 1, n) cin >> w[i];
vector<int> pw(n + 1);
FOR(i, 1, n) pw[i] = add(w[i], pw[i - 1]);
vector<int> ph(n + 1);
FOR(i, 1, n) ph[i] = add(h[i], ph[i - 1]);
vector<int> dp(n + 1);
stack<int> st; st.push(0);
FOR(i, 1, n){
while(h[i] <= h[st.top()]) st.pop();
dp[i] = add(dp[st.top()], mul(sub(pw[i], pw[st.top()]), h[i]));
st.push(i);
}
int answer = 0;
FOR(i, 1, n) answer = add(answer, dp[i]);
while(sz(st) > 1) st.pop();
FOR(i, 1, n){
while(w[i] <= w[st.top()]) st.pop();
dp[i] = add(dp[st.top()], mul(sub(ph[i], ph[st.top()]), w[i]));
st.push(i);
}
FOR(i, 1, n) answer = add(answer, dp[i]);
cout << answer << "\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |