#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], pw[i]%=MOD;
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"; */
ans%=MOD;
(ans+=MOD)%=MOD;
assert(ans >= 0 && ans < MOD);
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t=1; //cin>>t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2548 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
7 ms |
3164 KB |
Output is correct |
4 |
Correct |
14 ms |
3812 KB |
Output is correct |
5 |
Correct |
14 ms |
3644 KB |
Output is correct |
6 |
Correct |
14 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
9 ms |
3180 KB |
Output is correct |
4 |
Correct |
17 ms |
3928 KB |
Output is correct |
5 |
Correct |
23 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2576 KB |
Output is correct |
4 |
Correct |
9 ms |
3160 KB |
Output is correct |
5 |
Correct |
17 ms |
3932 KB |
Output is correct |
6 |
Correct |
18 ms |
3932 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
9 ms |
3164 KB |
Output is correct |
10 |
Correct |
17 ms |
3924 KB |
Output is correct |
11 |
Correct |
17 ms |
3932 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2520 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2528 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2476 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2848 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2460 KB |
Output is correct |
11 |
Correct |
8 ms |
3760 KB |
Output is correct |
12 |
Correct |
16 ms |
5096 KB |
Output is correct |
13 |
Correct |
15 ms |
4692 KB |
Output is correct |
14 |
Correct |
14 ms |
4696 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
4 ms |
2908 KB |
Output is correct |
17 |
Correct |
12 ms |
4144 KB |
Output is correct |
18 |
Correct |
18 ms |
5732 KB |
Output is correct |
19 |
Correct |
18 ms |
5972 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
10 ms |
4188 KB |
Output is correct |
23 |
Correct |
17 ms |
5716 KB |
Output is correct |
24 |
Correct |
17 ms |
5912 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
2 ms |
2648 KB |
Output is correct |
31 |
Correct |
2 ms |
2796 KB |
Output is correct |
32 |
Correct |
9 ms |
3932 KB |
Output is correct |
33 |
Correct |
12 ms |
3864 KB |
Output is correct |
34 |
Correct |
20 ms |
5640 KB |
Output is correct |
35 |
Correct |
17 ms |
5212 KB |
Output is correct |
36 |
Correct |
18 ms |
5456 KB |
Output is correct |
37 |
Correct |
18 ms |
5468 KB |
Output is correct |
38 |
Correct |
1 ms |
2516 KB |
Output is correct |
39 |
Correct |
17 ms |
5456 KB |
Output is correct |
40 |
Correct |
17 ms |
5468 KB |
Output is correct |
41 |
Correct |
17 ms |
5324 KB |
Output is correct |
42 |
Correct |
23 ms |
5972 KB |
Output is correct |