#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int h[N], w[N], pref[N];
int l[N], r[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
}
for(int i=1;i<=n;i++){
cin>>w[i];
pref[i] = pref[i-1] + w[i];
}
vector<int> ss;
for(int i=1;i<=n;i++){
while(!ss.empty() && h[ss.back()] > h[i]){
ss.pop_back();
}
if(!ss.empty()) l[i] = ss.back();
else l[i] = 0;
ss.push_back(i);
}
ss.clear();
for(int i=n;i>0;i--){
while(!ss.empty() && h[ss.back()] >= h[i]){
ss.pop_back();
}
if(!ss.empty()) r[i] = ss.back();
else r[i] = n + 1;
ss.push_back(i);
}
int inv = 5e8 + 4;
int res = 0;
for(int i=1;i<=n;i++){
int L = (pref[i-1] - pref[l[i]]) % mod, R = (pref[r[i]-1] - pref[i]) % mod;
int T = (pref[r[i] - 1] - pref[l[i]]) % mod;
int tot = (T * 1ll * (T + 1) % mod - L * 1ll * (L + 1) % mod - R * 1ll * (R + 1) % mod + mod + mod) % mod;
tot = tot * 1ll * inv % mod;
//~ cout<<T<<" "<<L<<" "<<R<<"\n";
//~ T = (w[i] * 1ll * (w[i] + 1) / 2 % mod + L * 1ll * R % mod + w[i] * 1ll * (L + R) % mod) % mod;
//~ cout<<T<<" "<<tot<<"\n";
T = h[i] * 1ll * (h[i] + 1) / 2 % mod;
//~ cout<<tot<<" "<<T<<"\n";
res = (res + tot * 1ll * T) % mod;
}
cout<<res<<"\n";
}
/*
5
2 2 1 1 2
1 1 1 1 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
10 ms |
2600 KB |
Output is correct |
4 |
Correct |
20 ms |
4944 KB |
Output is correct |
5 |
Correct |
19 ms |
4612 KB |
Output is correct |
6 |
Correct |
17 ms |
4408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Correct |
11 ms |
2740 KB |
Output is correct |
4 |
Correct |
23 ms |
5132 KB |
Output is correct |
5 |
Correct |
26 ms |
5092 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
852 KB |
Output is correct |
4 |
Correct |
13 ms |
2728 KB |
Output is correct |
5 |
Correct |
22 ms |
5136 KB |
Output is correct |
6 |
Correct |
24 ms |
5076 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
12 ms |
2780 KB |
Output is correct |
10 |
Correct |
22 ms |
5064 KB |
Output is correct |
11 |
Correct |
22 ms |
5072 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
2520 KB |
Output is correct |
12 |
Correct |
21 ms |
4900 KB |
Output is correct |
13 |
Correct |
20 ms |
4556 KB |
Output is correct |
14 |
Correct |
18 ms |
4348 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
11 ms |
2796 KB |
Output is correct |
18 |
Correct |
23 ms |
5052 KB |
Output is correct |
19 |
Correct |
25 ms |
5072 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
12 ms |
2772 KB |
Output is correct |
23 |
Correct |
21 ms |
5064 KB |
Output is correct |
24 |
Correct |
21 ms |
5044 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
11 ms |
2220 KB |
Output is correct |
33 |
Correct |
12 ms |
2260 KB |
Output is correct |
34 |
Correct |
23 ms |
4180 KB |
Output is correct |
35 |
Correct |
22 ms |
4220 KB |
Output is correct |
36 |
Correct |
24 ms |
4232 KB |
Output is correct |
37 |
Correct |
23 ms |
4204 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
21 ms |
4116 KB |
Output is correct |
40 |
Correct |
21 ms |
4180 KB |
Output is correct |
41 |
Correct |
21 ms |
4420 KB |
Output is correct |
42 |
Correct |
22 ms |
4748 KB |
Output is correct |