#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 1e5;
const int MOD = 1e9 + 7;
int h[MAX_N], w[MAX_N], p[MAX_N];
vector <int> s;
int lgPut( int x, int n ) {
if ( n == 0 )
return 1;
int p = lgPut( x, n / 2 );
p = (long long)p * p % MOD;
if ( n % 2 == 1 )
p = (long long)p * x % MOD;
return p;
}
const int inv2 = (MOD + 1) / 2;
int nchoose2( int n ) {
return (long long)n * (n + 1) % MOD * inv2 % MOD;
}
signed main() {
int n;
cin >> n;
for ( int i = 0; i < n; i++ )
cin >> h[i];
for ( int i = 0; i < n; i++ )
cin >> w[i];
int ans = 0, sum = 0;
for ( int i = 0; i < n; i++ ) {
while ( !s.empty() && h[i] <= h[s.back()] ) {
sum = (sum - (long long)nchoose2( h[s.back()] ) * p[s.back()] % MOD + MOD) % MOD;
p[i] = (p[i] + p[s.back()]) % MOD;
s.pop_back();
}
s.push_back( i );
sum = (sum + (long long)nchoose2( h[i] ) * p[i]) % MOD;
ans = (ans + (long long)nchoose2( h[i] ) * nchoose2( w[i] )) % MOD;
ans = (ans + (long long)sum * w[i]) % MOD;
sum = (sum + (long long)nchoose2( h[i] ) * w[i]) % MOD;
p[i] += w[i];
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
20 ms |
2652 KB |
Output is correct |
4 |
Correct |
40 ms |
2608 KB |
Output is correct |
5 |
Correct |
49 ms |
2592 KB |
Output is correct |
6 |
Correct |
45 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
6 ms |
2484 KB |
Output is correct |
3 |
Correct |
29 ms |
2480 KB |
Output is correct |
4 |
Correct |
56 ms |
2580 KB |
Output is correct |
5 |
Correct |
58 ms |
2584 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
6 ms |
2396 KB |
Output is correct |
4 |
Correct |
28 ms |
2644 KB |
Output is correct |
5 |
Correct |
56 ms |
2640 KB |
Output is correct |
6 |
Correct |
64 ms |
2580 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
6 ms |
2652 KB |
Output is correct |
9 |
Correct |
29 ms |
2980 KB |
Output is correct |
10 |
Correct |
54 ms |
3740 KB |
Output is correct |
11 |
Correct |
55 ms |
3788 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2512 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2496 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
20 ms |
3164 KB |
Output is correct |
12 |
Correct |
40 ms |
3864 KB |
Output is correct |
13 |
Correct |
40 ms |
3924 KB |
Output is correct |
14 |
Correct |
41 ms |
3924 KB |
Output is correct |
15 |
Correct |
2 ms |
2396 KB |
Output is correct |
16 |
Correct |
6 ms |
2652 KB |
Output is correct |
17 |
Correct |
29 ms |
3412 KB |
Output is correct |
18 |
Correct |
56 ms |
4516 KB |
Output is correct |
19 |
Correct |
58 ms |
4752 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
2940 KB |
Output is correct |
22 |
Correct |
35 ms |
3996 KB |
Output is correct |
23 |
Correct |
54 ms |
5580 KB |
Output is correct |
24 |
Correct |
55 ms |
5836 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
6 ms |
2652 KB |
Output is correct |
31 |
Correct |
6 ms |
2648 KB |
Output is correct |
32 |
Correct |
27 ms |
3420 KB |
Output is correct |
33 |
Correct |
29 ms |
3412 KB |
Output is correct |
34 |
Correct |
53 ms |
4436 KB |
Output is correct |
35 |
Correct |
53 ms |
4504 KB |
Output is correct |
36 |
Correct |
57 ms |
4484 KB |
Output is correct |
37 |
Correct |
63 ms |
4720 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
60 ms |
4612 KB |
Output is correct |
40 |
Correct |
56 ms |
4692 KB |
Output is correct |
41 |
Correct |
56 ms |
4688 KB |
Output is correct |
42 |
Correct |
56 ms |
5324 KB |
Output is correct |