#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(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
1112 KB |
Output is correct |
4 |
Correct |
14 ms |
1884 KB |
Output is correct |
5 |
Correct |
13 ms |
2036 KB |
Output is correct |
6 |
Correct |
13 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
1116 KB |
Output is correct |
4 |
Correct |
17 ms |
2040 KB |
Output is correct |
5 |
Correct |
18 ms |
2040 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Correct |
8 ms |
1116 KB |
Output is correct |
5 |
Correct |
17 ms |
2032 KB |
Output is correct |
6 |
Correct |
23 ms |
2040 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
8 ms |
1760 KB |
Output is correct |
10 |
Correct |
16 ms |
4056 KB |
Output is correct |
11 |
Correct |
17 ms |
4216 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 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 |
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 |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
7 ms |
1116 KB |
Output is correct |
12 |
Correct |
13 ms |
1880 KB |
Output is correct |
13 |
Correct |
14 ms |
1796 KB |
Output is correct |
14 |
Correct |
14 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
11 ms |
1116 KB |
Output is correct |
18 |
Correct |
16 ms |
1880 KB |
Output is correct |
19 |
Correct |
16 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
1900 KB |
Output is correct |
23 |
Correct |
16 ms |
4052 KB |
Output is correct |
24 |
Correct |
17 ms |
4056 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
8 ms |
1116 KB |
Output is correct |
33 |
Correct |
8 ms |
1112 KB |
Output is correct |
34 |
Correct |
15 ms |
1884 KB |
Output is correct |
35 |
Correct |
15 ms |
2040 KB |
Output is correct |
36 |
Correct |
16 ms |
1884 KB |
Output is correct |
37 |
Correct |
16 ms |
2048 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
16 ms |
2136 KB |
Output is correct |
40 |
Correct |
16 ms |
2036 KB |
Output is correct |
41 |
Correct |
16 ms |
2396 KB |
Output is correct |
42 |
Correct |
16 ms |
3036 KB |
Output is correct |