#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int MOD = 1e9+7;
ll h[N], w[N], pw[N], dp[N], n;
void solve() {
cin>>n;
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) pw[i]=pw[i-1]+w[i];
dp[0]=0, h[0]=0;
ll ans=0; // dp[i] -> from {1, 2, ..., i} ends on rightmost point of i
stack<int> st; st.push(0);
for (int i=1; i<=n; ++i) {
while (h[st.top()] > h[i]) st.pop();
ll hl=(h[i]*(h[i]+1)/2)%MOD;
// add all rects which start before (st.top())
(ans+=dp[st.top()]*w[i])%=MOD;
// add all rects which contains atleast one from w[i] and suffix of others
(ans+=(w[i]*(pw[i-1]-pw[st.top()]+1)%MOD)*hl)%=MOD;
(ans+=((w[i]*(w[i]-1)/2)%MOD)*hl)%=MOD;
(dp[i]+=dp[st.top()]+(pw[i]-pw[st.top()])*hl)%=MOD;
st.push(i);
}
/*
cout<<"dp: ";
for (int i=1; i<=n; ++i) cout<<dp[i]<<" ";
cout<<"\n"; */
assert(ans >= 0 && ans < MOD);
cout<<ans<<"\n";
}
int main() {
int t=1; //cin>>t;
while (t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
21 ms |
3644 KB |
Output is correct |
4 |
Correct |
41 ms |
4596 KB |
Output is correct |
5 |
Correct |
41 ms |
4432 KB |
Output is correct |
6 |
Incorrect |
41 ms |
4128 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
2652 KB |
Output is correct |
3 |
Correct |
30 ms |
3924 KB |
Output is correct |
4 |
Correct |
67 ms |
5204 KB |
Output is correct |
5 |
Correct |
59 ms |
5044 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
7 ms |
2748 KB |
Output is correct |
4 |
Correct |
31 ms |
3916 KB |
Output is correct |
5 |
Correct |
58 ms |
5204 KB |
Output is correct |
6 |
Correct |
63 ms |
5044 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
2776 KB |
Output is correct |
9 |
Correct |
30 ms |
3892 KB |
Output is correct |
10 |
Correct |
54 ms |
5056 KB |
Output is correct |
11 |
Correct |
60 ms |
5200 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2480 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |