#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vl vector<ll>
#define nikah ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll maxn = 1e5+7, modn1 = 1e9+7, modn2 = 998244353;
using namespace std;
// kalo kamu ngga emas nanti nadya merasa bersalah, ayo san demi nadya!!!
// Kalo emang soalnya susah, pindah soal dulu
// Jangan sampe diem, pikirin sesuatu, pasti bisa!!!
// Cobain semua approach, jangan ngestuck di satu pemikiran
// Jangan overthinking, jangan panik, tenangin pikiranmu
ll t,n;
ll h[maxn], w[maxn];
vector<ll>v,b;
ll f (ll x) {
return ((x * (x+1)) / 2) % modn1;
}
void solve () {
v.pb(0); b.pb(0);
cin>>n;
for (ll i=1; i<=n; i++) {
cin>>h[i];
}
for (ll i=1; i<=n; i++) {
cin>>w[i];
if (i>1) {
if (h[i] != h[i-1]) {
v.pb(h[i-1]);
b.pb(w[i-1]);
} else {
w[i] = (w[i] + w[i-1]) % modn1;
}
}
}
v.pb(h[n]); b.pb(w[n]);
ll ans = 0, cur = 1, cnt = 0, N = v.size();
bool inc = 0, dec = 0;
for (ll i=1; i<N; i++) {
if (i == N - 1) {
if (dec) {
ll cnt2 = 0;
for (ll j=i; j>=cur; j--) {
cnt2 += b[j];
if (j == i) ans = (ans + ((f(b[j]) * f(v[j])) % modn1)) % modn1;
else {
ans = (ans + ((f(cnt2) * f(v[j])) % modn1)) % modn1;
ans = (ans - ((f(cnt2 - b[j]) * f(v[j])) % modn1)) % modn1;
}
}
dec = 0;
} else {
cnt += b[i];
if (inc == 0) ans = (ans + ((f(b[i]) * f(v[i])) % modn1)) % modn1;
else {
ans = (ans + ((f(cnt) * f(v[i])) % modn1)) % modn1;
ans = (ans - ((f(cnt - b[i]) * f(v[i])) % modn1)) % modn1;
}
}
} else if (v[i] > v[i+1]) {
if (dec) {
ll cnt2 = 0;
for (ll j=i; j>=cur; j--) {
cnt2 += b[j];
if (j == i) ans = (ans + ((f(b[j]) * f(v[j])) % modn1)) % modn1;
else {
ans = (ans + ((f(cnt) * f(v[j])) % modn1)) % modn1;
ans = (ans - ((f(cnt - b[j]) * f(v[j])) % modn1)) % modn1;
}
}
dec = 0;
} else {
cnt += b[i];
if (inc == 0) ans = (ans + ((f(b[i]) * f(v[i])) % modn1)) % modn1;
else {
ans = (ans + ((f(cnt) * f(v[i])) % modn1)) % modn1;
ans = (ans - ((f(cnt - b[i]) * f(v[i])) % modn1)) % modn1;
}
}
inc = 1;
} else {
if (!dec) {
cur = i;
inc = 0;
cnt = 0;
}
dec = 1;
}
// cout<<i<<" : "<<ans<<endl;
}
while (ans < 0) ans += modn1;
cout<<ans<<endl;
}
int main () {
nikah
t = 1;
// cin>>t;
while (t--) {
solve();
// cout<<endl;
}
}
# |
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 |
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 |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
7 ms |
2140 KB |
Output is correct |
4 |
Correct |
14 ms |
3916 KB |
Output is correct |
5 |
Correct |
14 ms |
3932 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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
11 ms |
2140 KB |
Output is correct |
5 |
Correct |
15 ms |
3932 KB |
Output is correct |
6 |
Correct |
14 ms |
3932 KB |
Output is correct |
7 |
Incorrect |
0 ms |
476 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 |
0 ms |
348 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 |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |