#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxN = 1e5 + 7;
const int MOD = 1e9 + 7;
int pref[mxN], nex[mxN], add[mxN];
pair<int,int> arr[mxN];
int nc2(int n) {
return n * (n - 1) / 2;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> arr[i].first;
}
for (int i = 1; i <= n; ++i) {
cin >> arr[i].second;
pref[i] = pref[i - 1] + arr[i].second;
}
vector<int> st = {n + 1};
for (int i = n; i >= 1; --i) {
while (st.size() && arr[i].first <= arr[st.back()].first) {
st.pop_back();
}
assert(st.size());
nex[i] = st.back();
st.push_back(i);
}
int ans = 0, sub = 0;
for (int i = 1; i <= n; ++i) {
sub -= add[i];
int w = pref[nex[i] - 1] - pref[i - 1];
ans += (nc2(arr[i].first + 1) - sub) % MOD * nc2(w + 1) % MOD;
add[nex[i]] += (nc2(arr[i].first + 1) - sub);
sub = nc2(arr[i].first + 1);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |