#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;
}
cout << res;
return 0;
}
/*
2
2 2
2 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |