#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const int N = 100001;
ll mod = 1e9 + 7;
int h[N], R[N], L[N];
ll w[N];
ll pref[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
}
for (int i = 1; i <= n; ++i) {
cin >> w[i];
}
int id = 1;
for (int i = 1; i <= n;) {
int j = i;
ll sum = 0;
while (j <= n && h[j] == h[i]) {
sum += w[j];
j += 1;
}
h[id] = h[i];
w[id] = sum;
++id;
i = j;
}
n = id - 1;
vector<int> st;
for (int i = 1; i <= n; ++i) {
while (!st.empty() && h[st.back()] > h[i]) {
R[st.back()] = i;
st.pop_back();
}
if (st.empty()) {
L[i] = 0;
} else {
L[i] = st.back();
}
st.push_back(i);
}
for (int x : st) {
R[x] = n + 1;
}
for (int i = 1; i <= n; ++i) {
w[i] %= mod;
pref[i] = pref[i - 1] + w[i];
}
ll ans = 0;
const ll inv2 = (mod + 1) / 2;
for (int i = 1; i <= n; ++i) {
ll sumR = (pref[R[i] - 1] - pref[i]) % mod;
ll sumL = (pref[i - 1] - pref[L[i]]) % mod;
ll x = (h[i] * (ll)(h[i] + 1)) % mod * inv2 % mod;
ll now = (sumR * (sumL + w[i])) % mod;
now = (now + (sumL + w[i]) * (sumL + w[i] + 1) % mod * inv2 % mod - sumL * (sumL + 1) % mod * inv2 % mod) % mod;
now = (now + mod) % mod;
ans = (ans + now * x) % mod;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
10 ms |
2004 KB |
Output is correct |
4 |
Correct |
22 ms |
3188 KB |
Output is correct |
5 |
Correct |
24 ms |
3536 KB |
Output is correct |
6 |
Correct |
22 ms |
2924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
1852 KB |
Output is correct |
4 |
Correct |
20 ms |
3424 KB |
Output is correct |
5 |
Correct |
23 ms |
3448 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
580 KB |
Output is correct |
4 |
Correct |
10 ms |
1876 KB |
Output is correct |
5 |
Correct |
19 ms |
3408 KB |
Output is correct |
6 |
Correct |
23 ms |
3548 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
840 KB |
Output is correct |
9 |
Correct |
12 ms |
2388 KB |
Output is correct |
10 |
Correct |
30 ms |
5332 KB |
Output is correct |
11 |
Correct |
28 ms |
5440 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
368 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
2004 KB |
Output is correct |
12 |
Correct |
22 ms |
3276 KB |
Output is correct |
13 |
Correct |
20 ms |
3628 KB |
Output is correct |
14 |
Correct |
17 ms |
2992 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
11 ms |
1864 KB |
Output is correct |
18 |
Correct |
19 ms |
3376 KB |
Output is correct |
19 |
Correct |
22 ms |
3488 KB |
Output is correct |
20 |
Correct |
1 ms |
368 KB |
Output is correct |
21 |
Correct |
4 ms |
852 KB |
Output is correct |
22 |
Correct |
14 ms |
2364 KB |
Output is correct |
23 |
Correct |
30 ms |
5360 KB |
Output is correct |
24 |
Correct |
29 ms |
5456 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
368 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
716 KB |
Output is correct |
31 |
Correct |
4 ms |
728 KB |
Output is correct |
32 |
Correct |
14 ms |
2464 KB |
Output is correct |
33 |
Correct |
15 ms |
2648 KB |
Output is correct |
34 |
Correct |
28 ms |
4692 KB |
Output is correct |
35 |
Correct |
29 ms |
4724 KB |
Output is correct |
36 |
Correct |
30 ms |
4968 KB |
Output is correct |
37 |
Correct |
30 ms |
4992 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
30 ms |
5060 KB |
Output is correct |
40 |
Correct |
29 ms |
4940 KB |
Output is correct |
41 |
Correct |
31 ms |
5080 KB |
Output is correct |
42 |
Correct |
29 ms |
5304 KB |
Output is correct |