#include <bits/stdc++.h>
#include <fstream>
#define endl '\n'
#define mod 1000000007
#define INF 1000000000
#define INF2 2000000000000000000
//#define ll long long
///#define cin fin
///#define cout fout
using namespace std;
double const EPS = 1e-14;
///ofstream fout("herding.out");
///ifstream fin("herding.in");
int main()
{
ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
int n; cin >> n; long long h[n], w[n];
for(int i = 0; i < n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> w[i];
stack<pair<long long, long long>> st;
long long ans = 0, nw = 0;
for(int i = 0; i < n; i++) {
nw = 0;
while(!st.empty() && st.top().first > h[i]) {
pair<long long, long long> now = st.top();
nw += now.second;
nw %= mod;
st.pop();
ans += (((now.first*(now.first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod);
// cout << (((now.first*(now.first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod) << 'a' << endl;
ans %= mod;
if(!st.empty() && st.top().first > h[i]) {
ans -= ((st.top().first*(st.top().first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod;
// cout << ((st.top().first*(st.top().first+1))/2%mod) * ((nw*(nw+1))/2%mod) << 'b'<< endl;
ans += mod;
ans %= mod;
}
else {
ans -= ((h[i]*(h[i]+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod;
ans += mod;
ans %= mod;
}
}
st.push({h[i],w[i]+nw});
// cout << h[i] << ' ' << w[i]+nw << 's' << endl;
}
nw = 0;
while(!st.empty()) {
pair<long long, long long> now = st.top();
nw += now.second;
nw %= mod;
st.pop();
ans += (((now.first*(now.first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod);
// cout << (((now.first*(now.first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod) << 'c' << endl;
ans %= mod;
if(!st.empty()) {
ans -= ((st.top().first*(st.top().first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod;
// cout << ((st.top().first*(st.top().first+1))/2%mod) * ((nw*(nw+1))/2%mod)%mod << 'd' << endl;
ans += mod;
ans %= mod;
}
}
cout << ans%mod << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
8 ms |
2060 KB |
Output is correct |
4 |
Correct |
14 ms |
4020 KB |
Output is correct |
5 |
Correct |
15 ms |
3420 KB |
Output is correct |
6 |
Correct |
15 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
10 ms |
2768 KB |
Output is correct |
4 |
Correct |
23 ms |
4828 KB |
Output is correct |
5 |
Correct |
20 ms |
4700 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 |
9 ms |
2652 KB |
Output is correct |
5 |
Correct |
18 ms |
4688 KB |
Output is correct |
6 |
Correct |
18 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
856 KB |
Output is correct |
9 |
Correct |
12 ms |
2912 KB |
Output is correct |
10 |
Correct |
18 ms |
4696 KB |
Output is correct |
11 |
Correct |
18 ms |
4804 KB |
Output is correct |
12 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
352 KB |
Output is correct |
6 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
2076 KB |
Output is correct |
12 |
Correct |
19 ms |
4444 KB |
Output is correct |
13 |
Correct |
14 ms |
3676 KB |
Output is correct |
14 |
Correct |
14 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
9 ms |
2880 KB |
Output is correct |
18 |
Correct |
19 ms |
5468 KB |
Output is correct |
19 |
Correct |
19 ms |
5900 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
856 KB |
Output is correct |
22 |
Correct |
10 ms |
2908 KB |
Output is correct |
23 |
Correct |
18 ms |
5468 KB |
Output is correct |
24 |
Correct |
18 ms |
5456 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
9 ms |
1884 KB |
Output is correct |
33 |
Correct |
9 ms |
2136 KB |
Output is correct |
34 |
Correct |
17 ms |
3676 KB |
Output is correct |
35 |
Correct |
17 ms |
3792 KB |
Output is correct |
36 |
Correct |
17 ms |
3932 KB |
Output is correct |
37 |
Correct |
19 ms |
3792 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
19 ms |
4196 KB |
Output is correct |
40 |
Correct |
17 ms |
3932 KB |
Output is correct |
41 |
Correct |
17 ms |
3928 KB |
Output is correct |
42 |
Correct |
23 ms |
4772 KB |
Output is correct |