#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int M = 1e9 + 7;
void add(int &x, int y) {
if ((x += y) >= M) {
x -= M;
}
if (x < 0) {
x += M;
}
}
int C2(int x) {
return (long long) x * (x + 1) / 2 % M;
}
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;
}
vector<int> st;
int sum = 0, res = 0;
for (int i = 0; i < n; ++i) {
cin >> w[i];
int x = 0;
while (st.size() && h[st.back()] >= h[i]) {
add(x, w[st.back()]);
add(sum, (long long) -w[st.back()] * C2(h[st.back()]) % M);
st.pop_back();
}
add(res, (long long) w[i] * x % M * C2(h[i]) % M);
add(res, (long long) w[i] * sum % M);
add(res, (long long) C2(w[i]) * C2(h[i]) % M);
add(x, w[i]);
w[i] = x;
add(sum, (long long) w[i] * C2(h[i]) % M);
st.push_back(i);
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
408 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 |
7 ms |
1232 KB |
Output is correct |
4 |
Correct |
14 ms |
2396 KB |
Output is correct |
5 |
Correct |
16 ms |
2396 KB |
Output is correct |
6 |
Correct |
18 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
732 KB |
Output is correct |
3 |
Correct |
9 ms |
1628 KB |
Output is correct |
4 |
Correct |
18 ms |
3160 KB |
Output is correct |
5 |
Correct |
17 ms |
3152 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 |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
9 ms |
1628 KB |
Output is correct |
5 |
Correct |
26 ms |
3152 KB |
Output is correct |
6 |
Correct |
17 ms |
3164 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
1884 KB |
Output is correct |
10 |
Correct |
17 ms |
3396 KB |
Output is correct |
11 |
Correct |
18 ms |
3552 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
396 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
484 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
10 ms |
1352 KB |
Output is correct |
12 |
Correct |
14 ms |
2412 KB |
Output is correct |
13 |
Correct |
17 ms |
2396 KB |
Output is correct |
14 |
Correct |
16 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
1628 KB |
Output is correct |
18 |
Correct |
18 ms |
3028 KB |
Output is correct |
19 |
Correct |
17 ms |
3152 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 |
1884 KB |
Output is correct |
23 |
Correct |
17 ms |
3532 KB |
Output is correct |
24 |
Correct |
18 ms |
3552 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 |
488 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
11 ms |
1488 KB |
Output is correct |
33 |
Correct |
10 ms |
1628 KB |
Output is correct |
34 |
Correct |
19 ms |
2776 KB |
Output is correct |
35 |
Correct |
19 ms |
2900 KB |
Output is correct |
36 |
Correct |
20 ms |
3148 KB |
Output is correct |
37 |
Correct |
25 ms |
3164 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
22 ms |
3148 KB |
Output is correct |
40 |
Correct |
19 ms |
3152 KB |
Output is correct |
41 |
Correct |
19 ms |
3164 KB |
Output is correct |
42 |
Correct |
18 ms |
3308 KB |
Output is correct |