//IN THE NAME OF GOD
#include<bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
#define endl '\n'
#define F first
#define S second
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define pb push_back
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define file_io freopen("input.in" , "r" , stdin) ; freopen("output.out" , "w" , stdout);
using namespace std;
typedef long long ll;
typedef long double dll;
#define int long long
const int N = 1e5+7, mod = 1e9+7;
int h[N], w[N], sumw[N], dp[N], ans[N], inv2;
vector<pii> vec;
int power(int a, int b){
int ans = 1;
while(b){
if (b&1) ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
return ans;
}
int f(int x){
return (x * (x-1) % mod * inv2 % mod);
}
int32_t main(){
fast_io;
inv2 = power(2,mod-2);
int n; cin >> n;
for(int i=1; i<=n; i++) cin >> h[i];
for(int i=1; i<=n; i++){
cin >> w[i];
sumw[i] = (sumw[i-1] + w[i]) % mod;
}
dp[0] = ans[0] = 0;
vec.pb({0,0});
for(int i=1; i<=n; i++){
while(vec.back().F >= h[i]) vec.pop_back();
int ind = vec.back().S;
vec.pb({h[i],i});
int majw = (sumw[i-1] - sumw[ind] + mod) % mod, majw2 = (sumw[i] - sumw[ind] + mod)%mod;
dp[i] = (dp[ind] + f(h[i]+1) * majw2 % mod) %mod;
ans[i] = (dp[ind] * w[i] % mod + f(h[i]+1) * f(w[i]+1) % mod + f(h[i]+1) * w[i] % mod * majw % mod)%mod;
}
int out = 0;
for(int i=1; i<=n; i++) out = (out + ans[i]) % mod;
cout << out << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
2396 KB |
Output is correct |
4 |
Correct |
16 ms |
4188 KB |
Output is correct |
5 |
Correct |
16 ms |
4188 KB |
Output is correct |
6 |
Correct |
16 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
10 ms |
2228 KB |
Output is correct |
4 |
Correct |
19 ms |
4188 KB |
Output is correct |
5 |
Correct |
20 ms |
4188 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
10 ms |
2432 KB |
Output is correct |
5 |
Correct |
19 ms |
4188 KB |
Output is correct |
6 |
Correct |
20 ms |
4184 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
10 ms |
2780 KB |
Output is correct |
10 |
Correct |
21 ms |
7884 KB |
Output is correct |
11 |
Correct |
21 ms |
7888 KB |
Output is correct |
12 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
524 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
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 |
9 ms |
2908 KB |
Output is correct |
12 |
Correct |
16 ms |
5468 KB |
Output is correct |
13 |
Correct |
17 ms |
5456 KB |
Output is correct |
14 |
Correct |
17 ms |
5468 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
10 ms |
3160 KB |
Output is correct |
18 |
Correct |
20 ms |
6196 KB |
Output is correct |
19 |
Correct |
21 ms |
6228 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
1288 KB |
Output is correct |
22 |
Correct |
11 ms |
3804 KB |
Output is correct |
23 |
Correct |
21 ms |
7888 KB |
Output is correct |
24 |
Correct |
21 ms |
7896 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 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 |
3 ms |
964 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
10 ms |
3164 KB |
Output is correct |
33 |
Correct |
11 ms |
3164 KB |
Output is correct |
34 |
Correct |
19 ms |
5980 KB |
Output is correct |
35 |
Correct |
20 ms |
5856 KB |
Output is correct |
36 |
Correct |
21 ms |
6236 KB |
Output is correct |
37 |
Correct |
20 ms |
6236 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
20 ms |
6236 KB |
Output is correct |
40 |
Correct |
20 ms |
6136 KB |
Output is correct |
41 |
Correct |
24 ms |
6560 KB |
Output is correct |
42 |
Correct |
21 ms |
7128 KB |
Output is correct |