#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
using namespace std;
ll mod = 1e9+7;
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll N;
cin>>N;
vector<ll> prefix(N+1);
vector<ll> H(N+1), W(N+1);
ll res=0;
for(ll i=1;i<=N;i++) {
cin>>H[i];
}
for(ll i=1;i<=N;i++) {
cin>>W[i];
prefix[i]=prefix[i-1]+W[i];
}
vector<pair<ll,ll> > v(N);
set<ll> s;
for(ll i=1;i<=N;i++) {
s.insert(i);
v[i-1]=mp(H[i],i);
}
sort(v.rbegin(),v.rend());
s.insert(0);
s.insert(N+1);
for(ll t=0;t<N;t++) {
ll index=v[t].second;
s.erase(index);
auto it= s.upper_bound(index);
ll wd = prefix[*it-1]-prefix[index];
it=prev(it,1);
ll ws = prefix[index-1]-prefix[*it];
wd%=mod;
ws%=mod;
ll w = W[index];
ll h = H[index];
ll sum=h*(h+(ll)1)/(ll)2;
ll sumw=w*(w+(ll)1)/(ll)2;
sum%=mod;
sumw%=mod;
ll toadd=0;
toadd+=(ws*w)%mod;
toadd%=mod;
toadd+=(wd*w)%mod;
toadd%=mod;
toadd+=(wd*ws)%mod;
toadd%=mod;
toadd+=sumw;
toadd%=mod;
toadd*=sum;
toadd%=mod;
if(toadd<0) {
while(true) {
};
}
res+=toadd;
res%=mod;
}
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
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 |
26 ms |
4696 KB |
Output is correct |
4 |
Correct |
52 ms |
9052 KB |
Output is correct |
5 |
Correct |
61 ms |
8788 KB |
Output is correct |
6 |
Correct |
54 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
1184 KB |
Output is correct |
3 |
Correct |
29 ms |
4692 KB |
Output is correct |
4 |
Correct |
59 ms |
8784 KB |
Output is correct |
5 |
Correct |
63 ms |
8976 KB |
Output is correct |
6 |
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 |
5 ms |
1116 KB |
Output is correct |
4 |
Correct |
32 ms |
4684 KB |
Output is correct |
5 |
Correct |
63 ms |
8788 KB |
Output is correct |
6 |
Correct |
61 ms |
8936 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
1116 KB |
Output is correct |
9 |
Correct |
32 ms |
4684 KB |
Output is correct |
10 |
Correct |
58 ms |
9040 KB |
Output is correct |
11 |
Correct |
70 ms |
9040 KB |
Output is correct |
12 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
30 ms |
4692 KB |
Output is correct |
12 |
Correct |
67 ms |
8792 KB |
Output is correct |
13 |
Correct |
61 ms |
8784 KB |
Output is correct |
14 |
Correct |
59 ms |
9040 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
1176 KB |
Output is correct |
17 |
Correct |
34 ms |
4684 KB |
Output is correct |
18 |
Correct |
69 ms |
9040 KB |
Output is correct |
19 |
Correct |
69 ms |
9040 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
21 |
Correct |
6 ms |
1116 KB |
Output is correct |
22 |
Correct |
40 ms |
4732 KB |
Output is correct |
23 |
Correct |
73 ms |
9044 KB |
Output is correct |
24 |
Correct |
69 ms |
8936 KB |
Output is correct |
25 |
Correct |
0 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 |
8 ms |
1148 KB |
Output is correct |
31 |
Correct |
8 ms |
1116 KB |
Output is correct |
32 |
Correct |
44 ms |
4700 KB |
Output is correct |
33 |
Correct |
54 ms |
4944 KB |
Output is correct |
34 |
Correct |
101 ms |
9016 KB |
Output is correct |
35 |
Correct |
91 ms |
9040 KB |
Output is correct |
36 |
Correct |
100 ms |
9040 KB |
Output is correct |
37 |
Correct |
102 ms |
8788 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
97 ms |
8936 KB |
Output is correct |
40 |
Correct |
84 ms |
8788 KB |
Output is correct |
41 |
Correct |
74 ms |
8788 KB |
Output is correct |
42 |
Correct |
84 ms |
8940 KB |
Output is correct |