import sys, collections
input = sys.stdin.readline
n = int(input())
h = [*map(int, input().split())]
w = [*map(int, input().split())]
parent = [i for i in range(n)]
sz = [1] * n
ans = 0
def root(x):
if x == parent[x]:
return parent[x]
parent[x] = root(parent[x])
return parent[x]
def join(x, y):
global ans
x, y = root(x), root(y)
if x == y:
return
ans += w[x]*w[y]
if sz[x] > sz[y]:
x, y = y, x
parent[x] = y
sz[y] += sz[x]
w[y] += w[x]
d = collections.defaultdict(list)
for i in range(n):
d[h[i]].append(i)
res = 0
added = [False]*n
heights = sorted(d.keys(), reverse=True) + [0]
for height, nextheight in zip(heights, heights[1:]):
for i in d[height]:
ans += w[i]*(w[i]+1)//2
added[i] = True
if i and added[i-1]:
join(i-1, i)
if i+1 < n and added[i+1]:
join(i, i+1)
res += ans * (height*(height+1)//2 - nextheight*(nextheight+1)//2)
res %= 1000000007
print(res)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18636 KB |
Output is correct |
2 |
Correct |
53 ms |
20016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
18604 KB |
Output is correct |
2 |
Correct |
43 ms |
18580 KB |
Output is correct |
3 |
Correct |
43 ms |
18560 KB |
Output is correct |
4 |
Correct |
41 ms |
18520 KB |
Output is correct |
5 |
Correct |
38 ms |
18636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
18576 KB |
Output is correct |
2 |
Correct |
48 ms |
19492 KB |
Output is correct |
3 |
Correct |
133 ms |
30000 KB |
Output is correct |
4 |
Correct |
134 ms |
36632 KB |
Output is correct |
5 |
Correct |
162 ms |
36004 KB |
Output is correct |
6 |
Correct |
163 ms |
36420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
19548 KB |
Output is correct |
2 |
Correct |
59 ms |
22396 KB |
Output is correct |
3 |
Correct |
83 ms |
28868 KB |
Output is correct |
4 |
Correct |
107 ms |
35960 KB |
Output is correct |
5 |
Correct |
109 ms |
42028 KB |
Output is correct |
6 |
Correct |
38 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
18748 KB |
Output is correct |
2 |
Correct |
48 ms |
19528 KB |
Output is correct |
3 |
Correct |
61 ms |
22440 KB |
Output is correct |
4 |
Correct |
84 ms |
28908 KB |
Output is correct |
5 |
Correct |
106 ms |
35968 KB |
Output is correct |
6 |
Correct |
110 ms |
42080 KB |
Output is correct |
7 |
Correct |
52 ms |
19900 KB |
Output is correct |
8 |
Correct |
70 ms |
24464 KB |
Output is correct |
9 |
Correct |
112 ms |
30660 KB |
Output is correct |
10 |
Correct |
169 ms |
45128 KB |
Output is correct |
11 |
Correct |
161 ms |
45636 KB |
Output is correct |
12 |
Correct |
39 ms |
18568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
18600 KB |
Output is correct |
2 |
Correct |
55 ms |
20024 KB |
Output is correct |
3 |
Correct |
42 ms |
18652 KB |
Output is correct |
4 |
Correct |
38 ms |
18660 KB |
Output is correct |
5 |
Correct |
38 ms |
18556 KB |
Output is correct |
6 |
Correct |
38 ms |
18620 KB |
Output is correct |
7 |
Correct |
42 ms |
18560 KB |
Output is correct |
8 |
Correct |
48 ms |
19408 KB |
Output is correct |
9 |
Correct |
49 ms |
19644 KB |
Output is correct |
10 |
Correct |
52 ms |
19872 KB |
Output is correct |
11 |
Correct |
39 ms |
18700 KB |
Output is correct |
12 |
Correct |
42 ms |
18680 KB |
Output is correct |
13 |
Correct |
52 ms |
19596 KB |
Output is correct |
14 |
Correct |
51 ms |
19828 KB |
Output is correct |
15 |
Correct |
51 ms |
19808 KB |
Output is correct |
16 |
Correct |
49 ms |
18608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18636 KB |
Output is correct |
2 |
Correct |
53 ms |
20016 KB |
Output is correct |
3 |
Correct |
39 ms |
18564 KB |
Output is correct |
4 |
Correct |
54 ms |
20012 KB |
Output is correct |
5 |
Correct |
41 ms |
18620 KB |
Output is correct |
6 |
Correct |
43 ms |
18604 KB |
Output is correct |
7 |
Correct |
41 ms |
18652 KB |
Output is correct |
8 |
Correct |
38 ms |
18628 KB |
Output is correct |
9 |
Correct |
38 ms |
18608 KB |
Output is correct |
10 |
Correct |
48 ms |
19504 KB |
Output is correct |
11 |
Correct |
135 ms |
29952 KB |
Output is correct |
12 |
Correct |
137 ms |
36672 KB |
Output is correct |
13 |
Correct |
164 ms |
35932 KB |
Output is correct |
14 |
Correct |
162 ms |
36372 KB |
Output is correct |
15 |
Correct |
49 ms |
19536 KB |
Output is correct |
16 |
Correct |
59 ms |
22324 KB |
Output is correct |
17 |
Correct |
80 ms |
28820 KB |
Output is correct |
18 |
Correct |
109 ms |
36148 KB |
Output is correct |
19 |
Correct |
105 ms |
35772 KB |
Output is correct |
20 |
Correct |
51 ms |
19808 KB |
Output is correct |
21 |
Correct |
69 ms |
24420 KB |
Output is correct |
22 |
Correct |
109 ms |
30700 KB |
Output is correct |
23 |
Correct |
200 ms |
45196 KB |
Output is correct |
24 |
Correct |
163 ms |
45616 KB |
Output is correct |
25 |
Correct |
39 ms |
18708 KB |
Output is correct |
26 |
Correct |
41 ms |
18780 KB |
Output is correct |
27 |
Correct |
51 ms |
19888 KB |
Output is correct |
28 |
Correct |
50 ms |
19800 KB |
Output is correct |
29 |
Correct |
51 ms |
19620 KB |
Output is correct |
30 |
Correct |
178 ms |
26700 KB |
Output is correct |
31 |
Correct |
176 ms |
26828 KB |
Output is correct |
32 |
Correct |
306 ms |
30336 KB |
Output is correct |
33 |
Correct |
379 ms |
41900 KB |
Output is correct |
34 |
Correct |
595 ms |
59916 KB |
Output is correct |
35 |
Correct |
429 ms |
37308 KB |
Output is correct |
36 |
Correct |
586 ms |
59664 KB |
Output is correct |
37 |
Correct |
652 ms |
49888 KB |
Output is correct |
38 |
Correct |
40 ms |
18624 KB |
Output is correct |
39 |
Correct |
346 ms |
43388 KB |
Output is correct |
40 |
Correct |
239 ms |
43804 KB |
Output is correct |
41 |
Correct |
187 ms |
45396 KB |
Output is correct |
42 |
Correct |
178 ms |
44968 KB |
Output is correct |