#include <bits/stdc++.h>
#define int long long
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
ll exp(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ans;
}
ll inv(ll a) { return exp(a, mod - 2); }
ll C(ll a) { return a * (a + 1) % mod * inv(2) % mod; }
ll S(ll l, ll r) { return (C(r%mod) - C((l-1+mod)%mod) + mod) % mod; }
struct DSU {
int n, comp;
vector<int> par, size, W;
void config(int _n) {
n = _n + 1;
comp = _n;
par.resize(n+1);
size.resize(n+1, 1);
W.resize(n+1);
for(int i=1; i<=n; i++) par[i] = i;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return 0;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
W[u] = (W[u] + W[v]) % mod;
par[v] = u;
return 1;
}
void setW(int p, int w) { W[get(p)] += w; }
ll getW(int p) { return W[get(p)]; }
};
struct Edge {
int a, b, w;
bool operator<(Edge &e) { return w > e.w;}
};
int32_t main() {
int n;
cin >> n;
vector<ll> h(n+1), w(n+1);
for(int i=1; i<=n; i++) cin >> h[i];
for(int i=1; i<=n; i++) cin >> w[i];
ll ans = 0;
for(int i=1; i<=n; i++) ans = (ans + C(w[i]) * C(h[i])) % mod;
DSU dsu; dsu.config(n);
for(int i=1; i<=n; i++) dsu.setW(i, w[i]);
vector<Edge> edges;
for(int i=1; i<n; i++) edges.push_back({ i, i + 1, min(h[i], h[i+1]) });
sort(edges.begin(), edges.end());
for(Edge &e : edges) {
ll L = dsu.getW(e.a), R = dsu.getW(e.b);
dsu.uni(e.a, e.b);
ans = (ans + (R + mod) % mod * C(e.w) % mod * (L + mod) % mod) % mod;
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
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 |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
40 ms |
4052 KB |
Output is correct |
4 |
Correct |
82 ms |
7504 KB |
Output is correct |
5 |
Correct |
82 ms |
7480 KB |
Output is correct |
6 |
Correct |
82 ms |
7484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
500 KB |
Output is correct |
2 |
Correct |
10 ms |
1308 KB |
Output is correct |
3 |
Correct |
48 ms |
3904 KB |
Output is correct |
4 |
Correct |
97 ms |
8136 KB |
Output is correct |
5 |
Correct |
102 ms |
8784 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
11 ms |
1304 KB |
Output is correct |
4 |
Correct |
49 ms |
4044 KB |
Output is correct |
5 |
Correct |
98 ms |
7368 KB |
Output is correct |
6 |
Correct |
109 ms |
7488 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
10 ms |
1560 KB |
Output is correct |
9 |
Correct |
49 ms |
4052 KB |
Output is correct |
10 |
Correct |
94 ms |
7504 KB |
Output is correct |
11 |
Correct |
99 ms |
7364 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
544 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
44 ms |
3900 KB |
Output is correct |
12 |
Correct |
79 ms |
8248 KB |
Output is correct |
13 |
Correct |
82 ms |
8656 KB |
Output is correct |
14 |
Correct |
82 ms |
7476 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
10 ms |
1308 KB |
Output is correct |
17 |
Correct |
49 ms |
4040 KB |
Output is correct |
18 |
Correct |
98 ms |
8508 KB |
Output is correct |
19 |
Correct |
101 ms |
7476 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
1308 KB |
Output is correct |
22 |
Correct |
49 ms |
3892 KB |
Output is correct |
23 |
Correct |
94 ms |
8644 KB |
Output is correct |
24 |
Correct |
99 ms |
8764 KB |
Output is correct |
25 |
Correct |
2 ms |
500 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
11 ms |
1304 KB |
Output is correct |
31 |
Correct |
11 ms |
1304 KB |
Output is correct |
32 |
Correct |
49 ms |
4040 KB |
Output is correct |
33 |
Correct |
52 ms |
4040 KB |
Output is correct |
34 |
Correct |
99 ms |
7368 KB |
Output is correct |
35 |
Correct |
100 ms |
7372 KB |
Output is correct |
36 |
Correct |
102 ms |
9036 KB |
Output is correct |
37 |
Correct |
110 ms |
7628 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
103 ms |
8252 KB |
Output is correct |
40 |
Correct |
100 ms |
7372 KB |
Output is correct |
41 |
Correct |
100 ms |
7532 KB |
Output is correct |
42 |
Correct |
101 ms |
7364 KB |
Output is correct |