#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <stack>
#include <numeric>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
ll asum(ll n)
{
return ((n * (n + 1)) / 2) % MOD;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
//freopen("/Users/timreizin/Downloads/sample/input1.txt", "r", stdin);
int n;
cin >> n;
vector<ll> h(n), w(n);
for (ll &i : h) cin >> i;
for (ll &i : w) cin >> i;
vector<ll> pref(n + 1);
partial_sum(w.begin(), w.end(), pref.begin() + 1);
vector<int> sl(n, -1), sr(n, n);
stack<int> help;
for (int i = 0; i < n; ++i)
{
while (!help.empty() && h[help.top()] >= h[i]) help.pop();
if (!help.empty()) sl[i] = help.top();
help.push(i);
}
while (!help.empty()) help.pop();
for (int i = n - 1; i >= 0; --i)
{
while (!help.empty() && h[help.top()] > h[i]) help.pop();
if (!help.empty()) sr[i] = help.top();
help.push(i);
}
ll res = 0;
for (int i = 0; i < n; ++i)
{
/*if (i + 1 < n)
{
ll sum = w[i] * w[i + 1] % MOD - 1;
if (sum < 0) sum += MOD;
res = (res + sum * min(h[i], h[i + 1])) % MOD;
}*/
res = (res + asum(h[i]) * asum(w[i])) % MOD;
ll l1 = (pref[i] - pref[sl[i] + 1]) % MOD;
ll l2 = (pref[sr[i]] - pref[i + 1]) % MOD;
ll sum = (l1 + 1) * (l2 + 1) % MOD - 1;
if (sum < 0) sum += MOD;
res = (res + sum * asum(h[i])) % MOD;
l1 = (l1 * (w[i] - 1)) % MOD;
l2 = (l2 * (w[i] - 1)) % MOD;
res = (res + (l1 + l2) * asum(h[i])) % MOD;
}
cout << res;
return 0;
}
/*
2
2 2
2 2
*/
/*
2
1 2
1 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
11 ms |
2512 KB |
Output is correct |
4 |
Correct |
19 ms |
4948 KB |
Output is correct |
5 |
Correct |
21 ms |
4704 KB |
Output is correct |
6 |
Correct |
19 ms |
4572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
840 KB |
Output is correct |
3 |
Correct |
12 ms |
3020 KB |
Output is correct |
4 |
Correct |
23 ms |
5720 KB |
Output is correct |
5 |
Correct |
26 ms |
5828 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
13 ms |
3016 KB |
Output is correct |
5 |
Correct |
23 ms |
5716 KB |
Output is correct |
6 |
Correct |
26 ms |
5836 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
844 KB |
Output is correct |
9 |
Correct |
13 ms |
2892 KB |
Output is correct |
10 |
Correct |
26 ms |
5624 KB |
Output is correct |
11 |
Correct |
24 ms |
5720 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
240 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
11 ms |
2600 KB |
Output is correct |
12 |
Correct |
20 ms |
4936 KB |
Output is correct |
13 |
Correct |
20 ms |
4680 KB |
Output is correct |
14 |
Correct |
19 ms |
4584 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
840 KB |
Output is correct |
17 |
Correct |
13 ms |
3012 KB |
Output is correct |
18 |
Correct |
33 ms |
5712 KB |
Output is correct |
19 |
Correct |
30 ms |
5844 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
13 ms |
3004 KB |
Output is correct |
23 |
Correct |
29 ms |
5584 KB |
Output is correct |
24 |
Correct |
25 ms |
5744 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
712 KB |
Output is correct |
32 |
Correct |
12 ms |
2764 KB |
Output is correct |
33 |
Correct |
13 ms |
2768 KB |
Output is correct |
34 |
Correct |
23 ms |
5192 KB |
Output is correct |
35 |
Correct |
28 ms |
5228 KB |
Output is correct |
36 |
Correct |
26 ms |
5336 KB |
Output is correct |
37 |
Correct |
28 ms |
5376 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
30 ms |
5324 KB |
Output is correct |
40 |
Correct |
29 ms |
5320 KB |
Output is correct |
41 |
Correct |
25 ms |
5328 KB |
Output is correct |
42 |
Correct |
27 ms |
5580 KB |
Output is correct |