#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<ll>
#define vp vector<pp>
#define vvi vector<vi>
#define vvp vector<vp>
#define pp pair<ll, ll>
#define inf 1000000000000000
#define rep(i,n) for(ll i = 0; i < n; i++)
#define mod 1000000007
ll get(ll h, ll w){
h %= mod;
w %= mod;
return (((h * (h + 1)/ 2) % mod) * ((w * (w + 1) / 2) % mod) % mod);
}
int main(){
ll n;
cin >> n;
vi H(n + 1), W(n + 1);
rep(i,n){
cin >> H[i];
}
H[n] = 0; W[n] = 0;
rep(i,n) cin >> W[i];
vector<pp> q;
q.push_back({-1, -1});
ll ans = 0;
rep(i,n + 1){
ll akt = 0;
while(q.back().first >= H[i]){
pp seclast = q[q.size() - 2];
if(seclast.first >= H[i]){
ans += get(q.back().first, q.back().second);
ans -= get(seclast.first, q.back().second);
ans %= mod;
if(ans < 0) ans += mod;
ll www = q.back().second;
q.pop_back();
q.back().second += www;
}else{
ans += get(q.back().first, q.back().second);
ans -= get(H[i], q.back().second);
ans %= mod;
if(ans < 0) ans += mod;
akt = q.back().second;
q.pop_back();
}
ans %= mod;
}
q.push_back({H[i], W[i] + akt});
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
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 |
16 ms |
1232 KB |
Output is correct |
4 |
Correct |
33 ms |
1880 KB |
Output is correct |
5 |
Correct |
33 ms |
1884 KB |
Output is correct |
6 |
Correct |
34 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
348 KB |
Output is correct |
3 |
Correct |
24 ms |
1116 KB |
Output is correct |
4 |
Correct |
53 ms |
1884 KB |
Output is correct |
5 |
Correct |
51 ms |
1880 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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
588 KB |
Output is correct |
4 |
Correct |
24 ms |
1116 KB |
Output is correct |
5 |
Correct |
51 ms |
1884 KB |
Output is correct |
6 |
Correct |
50 ms |
1880 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
24 ms |
1740 KB |
Output is correct |
10 |
Correct |
46 ms |
4040 KB |
Output is correct |
11 |
Correct |
48 ms |
4036 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
360 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 |
348 KB |
Output is correct |
16 |
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 |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
20 ms |
1224 KB |
Output is correct |
12 |
Correct |
44 ms |
1884 KB |
Output is correct |
13 |
Correct |
37 ms |
1884 KB |
Output is correct |
14 |
Correct |
34 ms |
1880 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
584 KB |
Output is correct |
17 |
Correct |
38 ms |
1116 KB |
Output is correct |
18 |
Correct |
48 ms |
1884 KB |
Output is correct |
19 |
Correct |
50 ms |
1880 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
848 KB |
Output is correct |
22 |
Correct |
24 ms |
1604 KB |
Output is correct |
23 |
Correct |
51 ms |
4156 KB |
Output is correct |
24 |
Correct |
49 ms |
4040 KB |
Output is correct |
25 |
Correct |
1 ms |
344 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 |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
348 KB |
Output is correct |
32 |
Correct |
22 ms |
1116 KB |
Output is correct |
33 |
Correct |
24 ms |
1208 KB |
Output is correct |
34 |
Correct |
46 ms |
1884 KB |
Output is correct |
35 |
Correct |
45 ms |
1880 KB |
Output is correct |
36 |
Correct |
50 ms |
1884 KB |
Output is correct |
37 |
Correct |
48 ms |
1884 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
48 ms |
1884 KB |
Output is correct |
40 |
Correct |
49 ms |
1992 KB |
Output is correct |
41 |
Correct |
47 ms |
2252 KB |
Output is correct |
42 |
Correct |
49 ms |
3148 KB |
Output is correct |