#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
const ll mod=1e9+7;
int main() {
int n;
cin>>n;
vector<ll> h(n+1,0), w(n+1,0);
for(int i=0;i<n;++i) cin>>h[i];
for(int i=0;i<n;++i) cin>>w[i];
n++;
vector<pair<ll,ll>> t;
t.push_back({0,0});
ll ans=0;
for(int i=0;i<n;++i) {
ll sum=0;
while(t.back().f>h[i]) {
ll mn=max(h[i],t[t.size()-2].f);
ll mx=t.back().f-mn;
sum=(sum+t.back().s)%mod;
t.pop_back();
ans+=(mx*(mx+1)%mod*500000004%mod+mx*mn%mod)*(sum*(sum+1)%mod*500000004%mod)%mod;
ans%=mod;
}
if(t.back().f==h[i]) t.back().s=(t.back().s+sum+w[i])%mod;
else t.push_back({h[i], (sum+w[i])%mod});
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
20 ms |
1628 KB |
Output is correct |
4 |
Correct |
40 ms |
3156 KB |
Output is correct |
5 |
Correct |
40 ms |
3156 KB |
Output is correct |
6 |
Correct |
39 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
32 ms |
2272 KB |
Output is correct |
4 |
Correct |
55 ms |
3780 KB |
Output is correct |
5 |
Correct |
59 ms |
3932 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
636 KB |
Output is correct |
4 |
Correct |
36 ms |
2104 KB |
Output is correct |
5 |
Correct |
65 ms |
3748 KB |
Output is correct |
6 |
Correct |
58 ms |
3920 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
1116 KB |
Output is correct |
9 |
Correct |
30 ms |
2768 KB |
Output is correct |
10 |
Correct |
57 ms |
5804 KB |
Output is correct |
11 |
Correct |
78 ms |
6236 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
20 ms |
1600 KB |
Output is correct |
12 |
Correct |
39 ms |
3164 KB |
Output is correct |
13 |
Correct |
41 ms |
3316 KB |
Output is correct |
14 |
Correct |
39 ms |
3152 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
6 ms |
784 KB |
Output is correct |
17 |
Correct |
28 ms |
2136 KB |
Output is correct |
18 |
Correct |
56 ms |
3908 KB |
Output is correct |
19 |
Correct |
61 ms |
3892 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
1116 KB |
Output is correct |
22 |
Correct |
29 ms |
2764 KB |
Output is correct |
23 |
Correct |
57 ms |
5832 KB |
Output is correct |
24 |
Correct |
58 ms |
6076 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
6 ms |
636 KB |
Output is correct |
31 |
Correct |
6 ms |
604 KB |
Output is correct |
32 |
Correct |
27 ms |
2140 KB |
Output is correct |
33 |
Correct |
29 ms |
2132 KB |
Output is correct |
34 |
Correct |
54 ms |
3512 KB |
Output is correct |
35 |
Correct |
54 ms |
3640 KB |
Output is correct |
36 |
Correct |
58 ms |
3920 KB |
Output is correct |
37 |
Correct |
66 ms |
3936 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
59 ms |
3776 KB |
Output is correct |
40 |
Correct |
59 ms |
3896 KB |
Output is correct |
41 |
Correct |
58 ms |
4180 KB |
Output is correct |
42 |
Correct |
70 ms |
4912 KB |
Output is correct |