#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
const int MOD2 = 2 * MOD;
int md(int x) {
if (x < 0) return (x % MOD) + MOD;
if (x < MOD) return x;
return x % MOD;
}
int md2(int x) {
if (x < 0) return (x % MOD2) + MOD2;
if (x < MOD2) return x;
return x % MOD2;
}
int tri(int x) {
if (x % 2) return md((x + 1) / 2 * x);
return md(x / 2 * (x + 1));
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, minH, ans = 0, S = 0;
bool st3 = 1, st4 = 1, st5 = 1;
cin >> N;
vector<int> H(N), W(N);
for (int i = 0; i < N; i++) {
cin >> H[i];
if (H[i] > 2) st3 = 0;
if (H[i] != H[0]) st4 = 0;
if (i && H[i] < H[i - 1]) st5 = 0;
}
for (int i = 0; i < N; i++) {
cin >> W[i];
S = md2(S + W[i]);
}
if (st3) {
vector<int> compH{H[0]}, compW{W[0]};
for (int i = 1; i < N; i++) {
if (H[i] == compH.back()) compW.back() = md2(compW.back() + W[i]);
else {
compH.push_back(H[i]);
compW.push_back(W[i]);
}
}
for (int i = 0; i < (signed) compH.size(); i++) {
if (compH[i] == 2) {
ans = md(ans + md(2 * tri(compW[i])));
}
}
cout << md(ans + tri(S));
return 0;
}
if (st4) {
cout << md(tri(S) * tri(H[0]));
return 0;
}
if (st5) {
for (int i = N - 2; i >= 0; i--) {
W[i] = md2(W[i] + W[i + 1]);
}
for (int i = 0; i < N; i++) {
ans = md(ans + tri(H[i]) * tri(W[i]));
}
for (int i = 0; i < N - 1; i++) {
ans = md(ans - tri(H[i]) * tri(W[i + 1]));
}
cout << ans;
return 0;
}
if (N <= 1000) {
for (int i = 0; i < N; i++) {
minH = H[i];
ans = md(ans + md(H[i] * (H[i] + 1) / 2) * tri(W[i]));
for (int j = i + 1; j < N; j++) {
minH = min(minH, H[j]);
ans = md(ans + md(W[i] * W[j]) * tri(minH));
}
}
cout << ans;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
5 ms |
472 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 |
# |
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 |
8 ms |
1684 KB |
Output is correct |
4 |
Correct |
16 ms |
2520 KB |
Output is correct |
5 |
Correct |
17 ms |
3032 KB |
Output is correct |
6 |
Correct |
14 ms |
2420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
1116 KB |
Output is correct |
4 |
Correct |
18 ms |
1884 KB |
Output is correct |
5 |
Correct |
18 ms |
2032 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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
8 ms |
1248 KB |
Output is correct |
5 |
Correct |
16 ms |
2040 KB |
Output is correct |
6 |
Correct |
20 ms |
2044 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
1116 KB |
Output is correct |
10 |
Correct |
19 ms |
1880 KB |
Output is correct |
11 |
Correct |
33 ms |
1884 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
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 |
5 ms |
472 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1752 KB |
Output is correct |
12 |
Correct |
13 ms |
2520 KB |
Output is correct |
13 |
Correct |
14 ms |
3032 KB |
Output is correct |
14 |
Correct |
13 ms |
2416 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
1112 KB |
Output is correct |
18 |
Correct |
15 ms |
2040 KB |
Output is correct |
19 |
Correct |
16 ms |
1884 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
10 ms |
1116 KB |
Output is correct |
23 |
Correct |
19 ms |
2036 KB |
Output is correct |
24 |
Correct |
18 ms |
1884 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Incorrect |
2 ms |
856 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |