#include <bits/stdc++.h>
using ll = long long;
const ll MOD = 1e9+7;
typedef std::pair<ll,ll> pii;
ll gaussian(ll x){
return ((x*(x+1LL))/2LL)%MOD;
}
ll get_local(pii u){
return (gaussian(u.first)*gaussian(u.second))%MOD;
}
ll get_total(pii u){
return (gaussian(u.first)*u.second)%MOD;
}
int main()
{
ll ans=0;
ll temp=0;
int N;std::cin>>N;
long long a[N],b[N];
for(auto&x:a)std::cin>>x;
for(auto&x:b)std::cin>>x;
std::deque<pii> stack;
for(int i=0;i!=N;++i){
ll alpha=a[i],beta=b[i];
pii local = {alpha,beta};
ans=(ans+get_local(local))%MOD;
ll k=0;
while(stack.size()){
auto __ = stack.back();
if(__.first>=alpha){
k=(k+stack.back().second)%MOD;
temp-=get_total(stack.back());
if(temp<0)temp+=MOD;
local.second=(local.second+stack.back().second)%MOD;
stack.pop_back();
}else break;
}
ans=(ans+(((k*b[i])%MOD)*gaussian(alpha)))%MOD;
ans=((((temp*b[i])+ans)%MOD))%MOD;
stack.push_back(local);
temp=(temp+get_total(local))%MOD;
}
std::cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
31 ms |
1364 KB |
Output is correct |
4 |
Correct |
51 ms |
2196 KB |
Output is correct |
5 |
Correct |
54 ms |
2220 KB |
Output is correct |
6 |
Correct |
50 ms |
2116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
304 KB |
Output is correct |
2 |
Correct |
11 ms |
588 KB |
Output is correct |
3 |
Correct |
45 ms |
1432 KB |
Output is correct |
4 |
Correct |
71 ms |
2188 KB |
Output is correct |
5 |
Correct |
80 ms |
2200 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
9 ms |
588 KB |
Output is correct |
4 |
Correct |
40 ms |
1384 KB |
Output is correct |
5 |
Correct |
83 ms |
2208 KB |
Output is correct |
6 |
Correct |
77 ms |
2116 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
8 ms |
716 KB |
Output is correct |
9 |
Correct |
39 ms |
1716 KB |
Output is correct |
10 |
Correct |
70 ms |
3804 KB |
Output is correct |
11 |
Correct |
76 ms |
3864 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
288 KB |
Output is correct |
6 |
Correct |
0 ms |
248 KB |
Output is correct |
7 |
Correct |
0 ms |
396 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
2 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
292 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
26 ms |
1368 KB |
Output is correct |
12 |
Correct |
51 ms |
2116 KB |
Output is correct |
13 |
Correct |
57 ms |
2108 KB |
Output is correct |
14 |
Correct |
60 ms |
2132 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
8 ms |
540 KB |
Output is correct |
17 |
Correct |
39 ms |
1476 KB |
Output is correct |
18 |
Correct |
75 ms |
2116 KB |
Output is correct |
19 |
Correct |
81 ms |
2192 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
8 ms |
764 KB |
Output is correct |
22 |
Correct |
38 ms |
1732 KB |
Output is correct |
23 |
Correct |
73 ms |
3836 KB |
Output is correct |
24 |
Correct |
72 ms |
3780 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
296 KB |
Output is correct |
27 |
Correct |
1 ms |
292 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
9 ms |
588 KB |
Output is correct |
31 |
Correct |
8 ms |
544 KB |
Output is correct |
32 |
Correct |
34 ms |
1348 KB |
Output is correct |
33 |
Correct |
37 ms |
1420 KB |
Output is correct |
34 |
Correct |
68 ms |
2164 KB |
Output is correct |
35 |
Correct |
83 ms |
2224 KB |
Output is correct |
36 |
Correct |
79 ms |
2104 KB |
Output is correct |
37 |
Correct |
74 ms |
2180 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
74 ms |
2104 KB |
Output is correct |
40 |
Correct |
75 ms |
2184 KB |
Output is correct |
41 |
Correct |
77 ms |
2376 KB |
Output is correct |
42 |
Correct |
79 ms |
2988 KB |
Output is correct |