#include<bits/stdc++.h>
using namespace std;
using lli = long long;
const int mod = 1e9 + 7;
const int maxn = 1e6;
const int inv = 500000004;
lli h[maxn], w[maxn];
int main()
{
lli n;
cin >> n;
for (lli i = 1; i <= n; i ++)
{
cin >> h[i];
}
for (lli i = 1; i <= n; i ++)
{
cin >> w[i];
}
n ++;
vector<pair<lli,lli>> v;
v.push_back({0, 0});
lli ans = 0;
for(int i = 1; i <= n; i ++)
{
lli p = 0;
while(v.back().first > h[i])
{
lli m = max(h[i], v[(int)v.size() - 2].first);
lli q = v.back().first - m;
p = (p + v.back().second) % mod;
v.pop_back();
ans += (q * (q + 1) % mod * inv % mod + q * m % mod)*(p *(p + 1) % mod * inv % mod) % mod;
ans %= mod;
}
if(v.back().first == h[i])
{
v.back().second =(v.back().second + p + w[i]) % mod;
}else
{
v.push_back({h[i], (p + w[i]) % mod});
}
}
cout << ans % mod;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
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 |
0 ms |
2488 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 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 |
20 ms |
5212 KB |
Output is correct |
4 |
Correct |
39 ms |
7788 KB |
Output is correct |
5 |
Correct |
44 ms |
7772 KB |
Output is correct |
6 |
Correct |
39 ms |
7844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
7 ms |
2688 KB |
Output is correct |
3 |
Correct |
28 ms |
5468 KB |
Output is correct |
4 |
Correct |
60 ms |
8728 KB |
Output is correct |
5 |
Correct |
58 ms |
8612 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
6 ms |
2604 KB |
Output is correct |
4 |
Correct |
29 ms |
5468 KB |
Output is correct |
5 |
Correct |
56 ms |
8532 KB |
Output is correct |
6 |
Correct |
60 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
6 ms |
3160 KB |
Output is correct |
9 |
Correct |
29 ms |
6348 KB |
Output is correct |
10 |
Correct |
56 ms |
10776 KB |
Output is correct |
11 |
Correct |
62 ms |
10692 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 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 |
2504 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 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 |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2492 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
20 ms |
5212 KB |
Output is correct |
12 |
Correct |
41 ms |
7764 KB |
Output is correct |
13 |
Correct |
39 ms |
7772 KB |
Output is correct |
14 |
Correct |
39 ms |
7900 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
6 ms |
2652 KB |
Output is correct |
17 |
Correct |
32 ms |
5536 KB |
Output is correct |
18 |
Correct |
56 ms |
8656 KB |
Output is correct |
19 |
Correct |
67 ms |
8852 KB |
Output is correct |
20 |
Correct |
1 ms |
2408 KB |
Output is correct |
21 |
Correct |
7 ms |
3176 KB |
Output is correct |
22 |
Correct |
29 ms |
6372 KB |
Output is correct |
23 |
Correct |
56 ms |
10548 KB |
Output is correct |
24 |
Correct |
58 ms |
10860 KB |
Output is correct |
25 |
Correct |
0 ms |
2408 KB |
Output is correct |
26 |
Correct |
1 ms |
2408 KB |
Output is correct |
27 |
Correct |
1 ms |
2408 KB |
Output is correct |
28 |
Correct |
1 ms |
2528 KB |
Output is correct |
29 |
Correct |
2 ms |
2408 KB |
Output is correct |
30 |
Correct |
8 ms |
2664 KB |
Output is correct |
31 |
Correct |
6 ms |
2616 KB |
Output is correct |
32 |
Correct |
27 ms |
5472 KB |
Output is correct |
33 |
Correct |
30 ms |
5568 KB |
Output is correct |
34 |
Correct |
56 ms |
8276 KB |
Output is correct |
35 |
Correct |
54 ms |
8404 KB |
Output is correct |
36 |
Correct |
58 ms |
8528 KB |
Output is correct |
37 |
Correct |
74 ms |
9044 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
57 ms |
8532 KB |
Output is correct |
40 |
Correct |
58 ms |
8656 KB |
Output is correct |
41 |
Correct |
61 ms |
8916 KB |
Output is correct |
42 |
Correct |
67 ms |
9628 KB |
Output is correct |