#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000;
const int MOD = 1e9 + 7;
int dpLft[MAX], dpRght[MAX];
long long lft[MAX];
long long binpow(int a, int b){
if(!b) return 1;
long long x = binpow(a, b / 2); x = (x * x) % MOD;
if(b%2) return (x * a) % MOD;
else return x;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N; cin >> N;
vector<int> a(N);
for(auto &i : a) cin >> i;
vector<int> b(N);
for(auto &i : b) cin >> i;
for(int i = 0; i < N; i++){
if(a[i] > b[i]) swap(a[i], b[i]);
}
if(N <= MAX){
for(int i = 0; i < N; i++){
dpLft[i] = 0; dpRght[i] = 0; lft[i] = 2ll;
}
vector<pair<int, int>> v(2*N);
for(int i = 0; i < N; i++){
v[2*i] = {a[i], i};
v[2*i+1] = {b[i], i};
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
long long ans = 0;
vector<int> totalWater(N);
for(pair<int, int> pr : v){
int x = pr.first; int i = pr.second;
// cout << "x "<< x << " i "<< i << "\n";
long long ways = 1;
for(int j = 0; j < i; j++){
ways = (ways * lft[j]) % MOD;
}
for(int j = i+1; j < N; j++){
long long w = (ways * dpRght[j]) % MOD;
// cout << "j "<< j << " ways "<< ways << endl;
if(a[j] < x) {ans += w * (x - a[j]); ans %= MOD;}
if(b[j] < x) {ans += w * (x - b[j]); ans %= MOD;}
dpLft[j] += ways; dpLft[j] %= MOD;
ways = (ways * lft[j]) % MOD;
}
ways = 1;
for(int j = N-1; j > i; j--){
ways = (ways * lft[j]) % MOD;
}
for(int j = i-1; j >= 0; j--){
long long w = (ways * dpLft[j]) % MOD;
// cout << "j "<< j << " ways "<< ways << endl;
if(a[j] < x) {ans += w * (x - a[j]); ans %= MOD;}
if(b[j] < x) {ans += w * (x - b[j]); ans %= MOD;}
// if(a[i] < x) {totalWater[i] += w * (x - a[i]); totalWater[i] %= MOD;}
// if(b[i] < x) {totalWater[i] += w * (x - b[i]); totalWater[i] %= MOD;}
dpRght[j] += ways; dpRght[j] %= MOD;
ways = (ways * lft[j]) % MOD;
}
// for(int j = 0; j < N; j++){
// cout << "j "<< j << " lft "<< lft[j] << " dpL "<< dpLft[j] << " dpR "<< dpRght[j] << endl;
// }
lft[i]--;
}
cout << ans << "\n";
}else{
//Case <= 2.
long long ans = 0;
for(int i = 0; i < N; i++){
long long ways1 = (binpow(2, i) + MOD - 1) % MOD;
long long ways2 = (binpow(2, N - i - 1) + MOD - 1) % MOD;
ans += (ways1 * ways2) % MOD;
ans %= MOD;
}
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
460 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
2039 ms |
860 KB |
Output is correct |
28 |
Correct |
1909 ms |
956 KB |
Output is correct |
29 |
Correct |
1771 ms |
912 KB |
Output is correct |
30 |
Correct |
1747 ms |
856 KB |
Output is correct |
31 |
Correct |
1908 ms |
860 KB |
Output is correct |
32 |
Correct |
1874 ms |
860 KB |
Output is correct |
33 |
Correct |
1910 ms |
860 KB |
Output is correct |
34 |
Correct |
1893 ms |
916 KB |
Output is correct |
35 |
Correct |
1900 ms |
960 KB |
Output is correct |
36 |
Correct |
1840 ms |
860 KB |
Output is correct |
37 |
Correct |
1750 ms |
916 KB |
Output is correct |
38 |
Correct |
1777 ms |
860 KB |
Output is correct |
39 |
Correct |
1883 ms |
920 KB |
Output is correct |
40 |
Correct |
1864 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
460 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
456 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
2039 ms |
860 KB |
Output is correct |
50 |
Correct |
1909 ms |
956 KB |
Output is correct |
51 |
Correct |
1771 ms |
912 KB |
Output is correct |
52 |
Correct |
1747 ms |
856 KB |
Output is correct |
53 |
Correct |
1908 ms |
860 KB |
Output is correct |
54 |
Correct |
1874 ms |
860 KB |
Output is correct |
55 |
Correct |
1910 ms |
860 KB |
Output is correct |
56 |
Correct |
1893 ms |
916 KB |
Output is correct |
57 |
Correct |
1900 ms |
960 KB |
Output is correct |
58 |
Correct |
1840 ms |
860 KB |
Output is correct |
59 |
Correct |
1750 ms |
916 KB |
Output is correct |
60 |
Correct |
1777 ms |
860 KB |
Output is correct |
61 |
Correct |
1883 ms |
920 KB |
Output is correct |
62 |
Correct |
1864 ms |
860 KB |
Output is correct |
63 |
Correct |
2072 ms |
856 KB |
Output is correct |
64 |
Correct |
2093 ms |
1076 KB |
Output is correct |
65 |
Correct |
2154 ms |
1076 KB |
Output is correct |
66 |
Correct |
1997 ms |
920 KB |
Output is correct |
67 |
Correct |
1921 ms |
1072 KB |
Output is correct |
68 |
Correct |
1998 ms |
928 KB |
Output is correct |
69 |
Correct |
1971 ms |
992 KB |
Output is correct |
70 |
Correct |
1968 ms |
996 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1884 ms |
860 KB |
Output is correct |
11 |
Correct |
1808 ms |
920 KB |
Output is correct |
12 |
Correct |
1780 ms |
860 KB |
Output is correct |
13 |
Correct |
1869 ms |
860 KB |
Output is correct |
14 |
Correct |
112 ms |
6232 KB |
Output is correct |
15 |
Correct |
126 ms |
6324 KB |
Output is correct |
16 |
Correct |
132 ms |
6160 KB |
Output is correct |
17 |
Correct |
137 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
460 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
456 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
2039 ms |
860 KB |
Output is correct |
50 |
Correct |
1909 ms |
956 KB |
Output is correct |
51 |
Correct |
1771 ms |
912 KB |
Output is correct |
52 |
Correct |
1747 ms |
856 KB |
Output is correct |
53 |
Correct |
1908 ms |
860 KB |
Output is correct |
54 |
Correct |
1874 ms |
860 KB |
Output is correct |
55 |
Correct |
1910 ms |
860 KB |
Output is correct |
56 |
Correct |
1893 ms |
916 KB |
Output is correct |
57 |
Correct |
1900 ms |
960 KB |
Output is correct |
58 |
Correct |
1840 ms |
860 KB |
Output is correct |
59 |
Correct |
1750 ms |
916 KB |
Output is correct |
60 |
Correct |
1777 ms |
860 KB |
Output is correct |
61 |
Correct |
1883 ms |
920 KB |
Output is correct |
62 |
Correct |
1864 ms |
860 KB |
Output is correct |
63 |
Correct |
2072 ms |
856 KB |
Output is correct |
64 |
Correct |
2093 ms |
1076 KB |
Output is correct |
65 |
Correct |
2154 ms |
1076 KB |
Output is correct |
66 |
Correct |
1997 ms |
920 KB |
Output is correct |
67 |
Correct |
1921 ms |
1072 KB |
Output is correct |
68 |
Correct |
1998 ms |
928 KB |
Output is correct |
69 |
Correct |
1971 ms |
992 KB |
Output is correct |
70 |
Correct |
1968 ms |
996 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
412 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
0 ms |
360 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
1884 ms |
860 KB |
Output is correct |
81 |
Correct |
1808 ms |
920 KB |
Output is correct |
82 |
Correct |
1780 ms |
860 KB |
Output is correct |
83 |
Correct |
1869 ms |
860 KB |
Output is correct |
84 |
Correct |
112 ms |
6232 KB |
Output is correct |
85 |
Correct |
126 ms |
6324 KB |
Output is correct |
86 |
Correct |
132 ms |
6160 KB |
Output is correct |
87 |
Correct |
137 ms |
6236 KB |
Output is correct |
88 |
Incorrect |
149 ms |
13792 KB |
Output isn't correct |
89 |
Halted |
0 ms |
0 KB |
- |