#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 1e9 + 7;
constexpr int i2 = 500000004;
constexpr int i6 = 166666668;
int mul(int a, int b) {
return 1LL * a * b % mod;
}
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
void sub(int &a, int b) {
a -= b;
if (a < 0) {
a += mod;
}
}
int f(int n) {
return mul(i2, mul(n, n + 1));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> h(N), w(N);
for (int &x : h) {
cin >> x;
}
for (int &x : w) {
cin >> x;
}
int ans = 0, s = 0;
vector<array<int, 2>> stk;
for (int i = 0; i < N; i++) {
add(ans, mul(f(h[i]), f(w[i])));
int t = w[i];
while (!stk.empty() && stk.back()[0] >= h[i]) {
sub(s, mul(f(stk.back()[0]), stk.back()[1]));
add(t, stk.back()[1]);
stk.pop_back();
}
add(s, mul(f(h[i]), t));
add(ans, mul(w[i], s));
sub(ans, mul(f(h[i]), mul(w[i], w[i])));
stk.push_back({h[i], t});
}
cout << ans << "\n";
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
1216 KB |
Output is correct |
4 |
Correct |
18 ms |
2252 KB |
Output is correct |
5 |
Correct |
15 ms |
2280 KB |
Output is correct |
6 |
Correct |
15 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
10 ms |
1608 KB |
Output is correct |
4 |
Correct |
22 ms |
3012 KB |
Output is correct |
5 |
Correct |
22 ms |
3144 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
11 ms |
1728 KB |
Output is correct |
5 |
Correct |
18 ms |
3028 KB |
Output is correct |
6 |
Correct |
20 ms |
3144 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
732 KB |
Output is correct |
9 |
Correct |
9 ms |
2112 KB |
Output is correct |
10 |
Correct |
18 ms |
4024 KB |
Output is correct |
11 |
Correct |
18 ms |
4184 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
8 ms |
1232 KB |
Output is correct |
12 |
Correct |
17 ms |
2252 KB |
Output is correct |
13 |
Correct |
15 ms |
2252 KB |
Output is correct |
14 |
Correct |
18 ms |
2248 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
508 KB |
Output is correct |
17 |
Correct |
9 ms |
1608 KB |
Output is correct |
18 |
Correct |
18 ms |
3024 KB |
Output is correct |
19 |
Correct |
18 ms |
3144 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
3 ms |
792 KB |
Output is correct |
22 |
Correct |
14 ms |
1996 KB |
Output is correct |
23 |
Correct |
18 ms |
4040 KB |
Output is correct |
24 |
Correct |
18 ms |
4180 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
328 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
4 ms |
584 KB |
Output is correct |
32 |
Correct |
11 ms |
1476 KB |
Output is correct |
33 |
Correct |
9 ms |
1608 KB |
Output is correct |
34 |
Correct |
18 ms |
2760 KB |
Output is correct |
35 |
Correct |
27 ms |
2772 KB |
Output is correct |
36 |
Correct |
19 ms |
3020 KB |
Output is correct |
37 |
Correct |
19 ms |
3024 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
24 ms |
3016 KB |
Output is correct |
40 |
Correct |
24 ms |
3004 KB |
Output is correct |
41 |
Correct |
19 ms |
3148 KB |
Output is correct |
42 |
Correct |
21 ms |
3680 KB |
Output is correct |