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 ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
const int mod=1e9+7, maxn=2e5+5;
ll n, h[maxn], pf[maxn], w[maxn], ans, ki, ka, mid, res, leb[maxn];
ll deret(ll a, ll b) {
return ((b-a+1)*(a+b)/2)%mod;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
for(int i=1; i<=n; i++) {
cin >> h[i];
pf[i]=pf[i-1];
if(h[i]==2) pf[i]++;
}
for(int i=1; i<=n; i++) {
cin >> w[i];
leb[i]=leb[i-1]+w[i];
}
for(int i=1; i<=n; i++) {
ki=leb[n]-leb[i-1], ka=leb[n]-leb[i]+1;
ans+=deret(ka, ki), ans%=mod;
if(h[i]==2) {
ki=i, ka=n;
while(ki<=ka) {
mid=(ki+ka)/2;
if(pf[mid]-pf[i-1]==mid-i+1) res=mid, ki=mid+1;
else ka=mid-1;
}
ki=leb[res]-leb[i-1], ka=leb[res]-leb[i]+1;
ans+=2*deret(ka, ki);
ans%=mod;
}
}
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... |