#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()) sl[i] = help.top();
help.push(i);
}
ll res = 0;
for (int i = 0; i < n; ++i)
{
res = (res + asum(h[i]) * asum(w[i])) % MOD;
ll l1 = (pref[i] - pref[sl[i] + 1]) % MOD;
if (l1 < 0) l1 += MOD;
ll l2 = (pref[sr[i]] - pref[i + 1]) % MOD;
if (l2 < 0) l2 += 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |