#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
using namespace std;
const int MOD = 1e9 + 7;
int main(){
int n;
cin>>n;
vl visina, sirina;
bool all_same = true, sorted = true;
for(int i = 0; i < n; i++){
ll a;
cin>>a;
visina.pb(a);
if(i > 0 && visina[i] != visina[i-1]){
all_same = false;
}
if(i > 0 && visina[i] < visina[i-1]){
sorted = false;
}
}
for(int i = 0; i < n; i++){
ll a;
cin>>a;
sirina.pb(a);
}
ll rez = 0, cur = 0, sum = 0;
if(all_same){
ll vis = 0, sir = 0;
for(int i = 0; i < n; i++){
sir += sirina[i];
sir %= MOD;
}
vis = visina[0];
vis %= MOD;
sir %= MOD;
rez = sir * (sir + 1) / 2;
rez %= MOD;
ll rez2 = vis * (vis + 1) / 2;
rez2 %= MOD;
cout<<(rez*rez2)%MOD<<endl;
return 0;
}
if(sorted){
ll rez = 0, ss = 0;
vl sum;
sum.resize(n);
for(int i = n-1; i >=0;i--){
ss += sirina[i];
ss %= MOD;
sum[i] = ss;
if(sum[i] >= MOD || sum[i] < 0)
assert(false);
}
for(int i = 0; i < n; i++){
if(i == 0){
ll sir = sum[i];
sir %= MOD;
ll vis = visina[i];
vis %= MOD;
ll rez1 = vis * (vis + 1) / 2;
rez1 %= MOD;
ll rez2 = sir * (sir + 1) / 2;
rez2 %= MOD;
rez += (rez1 * rez2) % MOD;
rez %= MOD;
}
else{
ll sir = sum[i];
sir%=MOD;
ll vis1 = visina[i], vis2 = visina[i-1];
vis1 %= MOD;
vis2 %= MOD;
ll sega = sir *(sir + 1) / 2;
sega %= MOD;
ll sega1 = vis1*(vis1+1) / 2;
sega1%=MOD;
ll rez1 = (sega * sega1) % MOD;
ll sega2 = vis2*(vis2 + 1) / 2;
sega2%= MOD;
ll rez2 = (sega * sega2) % MOD;
rez2 %= MOD;
if(rez1 >= MOD || rez2 >= MOD)
assert(false);
if(rez1 < rez2)
rez1+=MOD;
ll add = (rez1 - rez2)%MOD;
if(add >= MOD || add < 0)
assert(false);
add %= MOD;
rez += add;
rez %= MOD;
}
}
cout<<rez%MOD<<endl;
return 0;
}
for(int i = 0; i < n; i++){
sum += sirina[i];
sum %= MOD;
if(visina[i] == 2)
{
cur += sirina[i];
cur %= MOD;
}
else{
if(cur > 0){
rez += cur * (cur + 1);
rez %= MOD;
cur = 0;
}
}
}
if(cur > 0){
rez += cur * (cur + 1);
rez %= MOD;
}
rez += sum * (sum + 1) / 2;
rez %= MOD;
cout<<rez<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
26 ms |
1356 KB |
Output is correct |
4 |
Correct |
54 ms |
2496 KB |
Output is correct |
5 |
Correct |
58 ms |
2532 KB |
Output is correct |
6 |
Correct |
61 ms |
2540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
7 ms |
492 KB |
Output is correct |
3 |
Correct |
40 ms |
1432 KB |
Output is correct |
4 |
Correct |
75 ms |
2516 KB |
Output is correct |
5 |
Correct |
80 ms |
2508 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
10 ms |
512 KB |
Output is correct |
4 |
Correct |
38 ms |
1352 KB |
Output is correct |
5 |
Correct |
73 ms |
2496 KB |
Output is correct |
6 |
Correct |
83 ms |
2588 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
724 KB |
Output is correct |
9 |
Correct |
40 ms |
2496 KB |
Output is correct |
10 |
Correct |
78 ms |
4588 KB |
Output is correct |
11 |
Correct |
85 ms |
4600 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |