#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n;
cin>>n;
ll h[n],w[n],MOD=1e9+7;
for(int i=0;i<n;i++)cin>>h[i];
for(int i=0;i<n;i++)cin>>w[i];
ll cw=0;
map<ll,vector<pll>> m;
for(int i=0;i<n;i++){
m[h[i]].pb({cw+1,cw+w[i]});
cw+=w[i];
}
set<pll> s;
ll pr=0,ans=0,sum=cw%MOD*(cw%MOD+1)/2;
sum%=MOD;
s.insert({1,cw});
for(auto[x,v]:m){
ll a=x-pr;
ans+=(a*(a+1)/2+a*pr)%MOD*sum;
ans%=MOD;
for(auto[l,r]:v){
auto it=s.lower_bound({l+1,0});
it--;
auto[l2,r2]=*it;
s.erase(it);
if(l2<l)s.insert({l2,l-1});
if(r<r2)s.insert({r+1,r2});
l%=MOD;
l2%=MOD;
r%=MOD;
r2%=MOD;
sum-=(r2-l2+1)*(r2-l2+2)/2;
sum+=(l-l2)*(l-l2+1)/2;
sum+=(r2-r)*(r2-r+1)/2;
sum%=MOD;
}
pr=x;
}
ans+=MOD;
ans%=MOD;
cout<<ans<<'\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
12 ms |
3732 KB |
Output is correct |
4 |
Correct |
25 ms |
6992 KB |
Output is correct |
5 |
Correct |
25 ms |
6996 KB |
Output is correct |
6 |
Correct |
19 ms |
6708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1192 KB |
Output is correct |
3 |
Correct |
11 ms |
3772 KB |
Output is correct |
4 |
Correct |
20 ms |
7124 KB |
Output is correct |
5 |
Correct |
21 ms |
7112 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
1248 KB |
Output is correct |
4 |
Correct |
10 ms |
3864 KB |
Output is correct |
5 |
Correct |
20 ms |
7112 KB |
Output is correct |
6 |
Correct |
20 ms |
7116 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
1744 KB |
Output is correct |
9 |
Correct |
15 ms |
5328 KB |
Output is correct |
10 |
Correct |
33 ms |
14672 KB |
Output is correct |
11 |
Correct |
37 ms |
14664 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
484 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
604 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 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 |
12 ms |
3712 KB |
Output is correct |
12 |
Correct |
25 ms |
6976 KB |
Output is correct |
13 |
Correct |
24 ms |
6988 KB |
Output is correct |
14 |
Correct |
20 ms |
6884 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1248 KB |
Output is correct |
17 |
Correct |
13 ms |
3820 KB |
Output is correct |
18 |
Correct |
21 ms |
7368 KB |
Output is correct |
19 |
Correct |
20 ms |
7244 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
1884 KB |
Output is correct |
22 |
Correct |
15 ms |
5332 KB |
Output is correct |
23 |
Correct |
32 ms |
14684 KB |
Output is correct |
24 |
Correct |
36 ms |
14676 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
604 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 |
1884 KB |
Output is correct |
31 |
Correct |
6 ms |
1976 KB |
Output is correct |
32 |
Correct |
20 ms |
3676 KB |
Output is correct |
33 |
Correct |
38 ms |
8400 KB |
Output is correct |
34 |
Correct |
88 ms |
15700 KB |
Output is correct |
35 |
Correct |
41 ms |
6996 KB |
Output is correct |
36 |
Correct |
80 ms |
16208 KB |
Output is correct |
37 |
Correct |
77 ms |
12880 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
68 ms |
10344 KB |
Output is correct |
40 |
Correct |
57 ms |
14672 KB |
Output is correct |
41 |
Correct |
37 ms |
14652 KB |
Output is correct |
42 |
Correct |
48 ms |
14672 KB |
Output is correct |