This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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]) % MOD;
ans += (nc2(arr[i].first + 1) - sub) % MOD * (nc2(w + 1) % MOD) % 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |