#pragma GCC optimize "O3"
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef tuple<ll,ll,ll> trio;
typedef pair<int,int> pii;
const int MAXN = 1e5+10;
const ll MOD = 1e9+7;
#define all(x) x.begin(), x.end()
#define mk make_pair
#define pb push_back
#define f first
#define s second
ll n, resp, h[MAXN], w[MAXN], ant[MAXN], pos[MAXN], pw[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> h[i];
for(int i = 1; i <= n; i++)
cin >> w[i], pw[i] = pw[i-1] + w[i];
for(int i = 1; i <= n; i++)
ant[i] = i-1, pos[i] = i+1;
for(int i = 1; i <= n; i++) {
while(h[i] <= h[ant[i]]) ant[i] = ant[ant[i]];
while(h[i] < h[pos[i]]) pos[i] = pos[pos[i]];
}
for(int i = 1; i <= n; i++) {
ll l = ant[i]+1, r = pos[i]-1;
ll tot = (((pw[r]-pw[l-1]+1)%MOD) * ((pw[r]-pw[l-1])%MOD))/2;
ll casoA = (((pw[r]-pw[i]+1)%MOD) * ((pw[r]-pw[i])%MOD))/2;
ll casoB = (((pw[i-1]-pw[l-1]+1)%MOD) * ((pw[i-1]-pw[l-1])%MOD))/2;
resp += (tot%MOD - casoA%MOD - casoB%MOD)%MOD * ((w[i] * (w[i]+1) / 2)%MOD);
resp %= MOD;
}
cout << (resp + MOD) % MOD;
}
// g++ -std=c++17 clara.cpp -o clara
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2512 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |