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 MOD = 1e9+7;
void solve() {
int n;
cin >> n;
pair<int,int> nums[n];
int mx = 0;
bool b = 1, b2 = 1;
for (int i = 0;i < n;i++) {
cin >> nums[i].first;
mx = max(mx, nums[i].first);
}
for (int i = 1;i < n;i++) {
if (nums[i].first != nums[i-1].first) {
b = false;
}
if (nums[i].first < nums[i-1].first) {
b2 = false;
}
}
for (int i = 0;i < n;i++) {
cin >> nums[i].second;
}
int ans = 0;
if (b) {
int len = 0;
for (int i = 0;i < n;i++) {
len += nums[i].second;
}
len%= MOD;
ans = (((mx)*(mx+1)/2)%MOD)*(((len)*(len+1)/2)%MOD);
ans %= MOD;
cout << ans;
return;
}
if (b2) {
int suf[n];
suf[n-1] = nums[n-1].second;
for (int i = n-2;i >= 0;i--) {
suf[i] = (suf[i+1] + nums[i].second)%MOD;
}
int last = 0;
int ans = 0;
for (int i = 0;i < n;i++) {
int x = ( (((nums[i].first+1)* (nums[i].first)/2)%MOD) - (((last+1)* (last)/2)%MOD) + MOD )%MOD;
int y = (((suf[i]+1)*(suf[i])/2)%MOD);
ans = (ans + (x*y))%MOD;
last = nums[i].first;
}
cout << ans;
return;
}
for (int h = 1;h <= mx;h++) {
int len = 0;
for (int i = 0;i < n;i++) {
if (nums[i].first < h) {
len %= MOD;
ans = (ans + (h * (((len)*(len+1)/2)%MOD)))%MOD;
len = 0;
} else {
len += nums[i].second;
}
}
len %= MOD;
ans = (ans + ((h) * (((len)*(len+1)/2)%MOD)))%MOD;
len = 0;
}
cout << ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while (tc--) {
solve();
}
}
# | 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... |