#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
ll dp[100001];
const ll linf = 1e18 + 1;
const ll mod = 1e9 + 7;
int main(){
int n; cin >> n;
ll h[n + 1], w[n + 1];
for(int i = 1; i <= n; i++) cin >> h[i];
for(int i = 1; i <= n; i++){
cin >> w[i];
}
ll pref[n + 1];
pref[0] = 0;
for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + w[i];
stack <int> st;
st.push(1);
int l[n + 1], r[n + 1];
l[1] = 0;
for(int i = 2; i <= n; i++){
while(!st.empty()){
int v = st.top();
if(h[v] <= h[i]) break;
st.pop();
}
if(st.empty()) l[i] = 0;
else l[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
r[n] = n + 1;
st.push(n);
for(int i= n - 1; i >= 1; i--){
while(!st.empty()){
int v = st.top();
if(h[v] < h[i]) break;
st.pop();
}
if(st.empty()) r[i] = n + 1;
else r[i] = st.top();
st.push(i);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ll x = h[i] * (h[i] - 1) / 2 + h[i]; x %= mod;
int L = l[i], R = r[i];
ll y = pref[R - 1] - pref[L];
ll y1 = y;
if(y % 2 == 0){
ll x = y - 1; x %= mod;
y /= 2;
y %= mod;
y *= x; y %= mod;
y += y1; y %= mod;
}
else{
ll v = y - 1; v /= 2;
v %= mod;
v *= y % mod;
v %= mod;
y = v;
y += y1; y %= mod;
}
ans += x * y;
ans %= mod;
y = pref[i - 1] - pref[L];
y1 = y;
if(y % 2 == 0){
ll x = y - 1; x %= mod;
y /= 2;
y %= mod;
y *= x; y %= mod;
y += y1; y %= mod;
}
else{
ll v = y - 1; v /= 2;
v %= mod;
v *= y % mod;
v %= mod;
y = v;
y += y1;
y %= mod;
}
ans -= x * y; ans %= mod; ans += mod; ans %= mod;
y = pref[R - 1] - pref[i];
y1 = y;
if(y % 2 == 0){
ll x = y - 1; x %= mod;
y /= 2;
y %= mod;
y *= x; y %= mod;
y += y1; y %= mod;
}
else{
ll v = y - 1; v /= 2;
v %= mod;
v *= y % mod;
v %= mod;
y = v;
y += y1; y %= mod;
}
ans -= x * y; ans %= mod; ans += mod; ans %= mod;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
29 ms |
2436 KB |
Output is correct |
4 |
Correct |
59 ms |
4916 KB |
Output is correct |
5 |
Correct |
59 ms |
4656 KB |
Output is correct |
6 |
Correct |
55 ms |
4536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
852 KB |
Output is correct |
3 |
Correct |
41 ms |
2952 KB |
Output is correct |
4 |
Correct |
81 ms |
5680 KB |
Output is correct |
5 |
Correct |
96 ms |
5804 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
852 KB |
Output is correct |
4 |
Correct |
43 ms |
2980 KB |
Output is correct |
5 |
Correct |
80 ms |
5676 KB |
Output is correct |
6 |
Correct |
102 ms |
5816 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
840 KB |
Output is correct |
9 |
Correct |
41 ms |
2968 KB |
Output is correct |
10 |
Correct |
74 ms |
5544 KB |
Output is correct |
11 |
Correct |
78 ms |
5692 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
2 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
27 ms |
2496 KB |
Output is correct |
12 |
Correct |
57 ms |
4912 KB |
Output is correct |
13 |
Correct |
77 ms |
4652 KB |
Output is correct |
14 |
Correct |
54 ms |
4660 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
828 KB |
Output is correct |
17 |
Correct |
43 ms |
3020 KB |
Output is correct |
18 |
Correct |
84 ms |
5692 KB |
Output is correct |
19 |
Correct |
86 ms |
5808 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
8 ms |
852 KB |
Output is correct |
22 |
Correct |
44 ms |
3000 KB |
Output is correct |
23 |
Correct |
73 ms |
5580 KB |
Output is correct |
24 |
Correct |
86 ms |
5708 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
11 ms |
780 KB |
Output is correct |
31 |
Correct |
8 ms |
724 KB |
Output is correct |
32 |
Correct |
38 ms |
2732 KB |
Output is correct |
33 |
Correct |
39 ms |
2736 KB |
Output is correct |
34 |
Correct |
75 ms |
5164 KB |
Output is correct |
35 |
Correct |
79 ms |
5056 KB |
Output is correct |
36 |
Correct |
95 ms |
5324 KB |
Output is correct |
37 |
Correct |
80 ms |
5304 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
78 ms |
5296 KB |
Output is correct |
40 |
Correct |
81 ms |
5296 KB |
Output is correct |
41 |
Correct |
84 ms |
5300 KB |
Output is correct |
42 |
Correct |
83 ms |
5544 KB |
Output is correct |