#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();
(ans+=dp[st.top()]*w[i])%=MOD;
ll hl=(h[i]*(h[i]+1)/2)%MOD, wl=(w[i]*(w[i]+1)/2)%MOD;
(dp[i]+=dp[st.top()]+(pw[i]-pw[st.top()])*hl)%=MOD;
(ans+=((w[i]*(pw[i-1]-pw[st.top()]))%MOD)*hl)%=MOD;
(ans+=wl*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 |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2492 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
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 |
2504 KB |
Output is correct |
3 |
Correct |
21 ms |
3752 KB |
Output is correct |
4 |
Correct |
41 ms |
5000 KB |
Output is correct |
5 |
Correct |
41 ms |
4692 KB |
Output is correct |
6 |
Incorrect |
41 ms |
4692 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
6 ms |
2880 KB |
Output is correct |
3 |
Correct |
34 ms |
4192 KB |
Output is correct |
4 |
Correct |
57 ms |
5800 KB |
Output is correct |
5 |
Correct |
59 ms |
5820 KB |
Output is correct |
6 |
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 |
9 ms |
2652 KB |
Output is correct |
4 |
Correct |
29 ms |
4160 KB |
Output is correct |
5 |
Correct |
61 ms |
5716 KB |
Output is correct |
6 |
Correct |
61 ms |
5924 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
7 ms |
2884 KB |
Output is correct |
9 |
Correct |
29 ms |
4212 KB |
Output is correct |
10 |
Correct |
55 ms |
5712 KB |
Output is correct |
11 |
Correct |
57 ms |
5800 KB |
Output is correct |
12 |
Correct |
1 ms |
2696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 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 |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |