#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 % MOD;
}
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]) % MOD;
ans += (nc2(arr[i].first + 1) - sub) % MOD * nc2(w + 1) % MOD;
ans %= 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 |
344 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 |
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 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
12 ms |
3460 KB |
Output is correct |
4 |
Correct |
17 ms |
4184 KB |
Output is correct |
5 |
Correct |
17 ms |
4188 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
600 KB |
Output is correct |
4 |
Correct |
9 ms |
3356 KB |
Output is correct |
5 |
Correct |
17 ms |
4188 KB |
Output is correct |
6 |
Correct |
19 ms |
4188 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
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 |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |