#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
namespace x = __gnu_pbds;
template <typename T>
using ordered_set = x::tree<T, x::null_type, less<T>, x::rb_tree_tag, x::tree_order_statistics_node_update>;
template <typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
#define x first
#define y second
using ll = long long;
using ld = long double;
const int N = 510;
struct DSU {
int rank[N], parent[N];
DSU() {
clear();
}
void clear() {
fill(all(rank), 0);
iota(all(parent), 0);
}
int get(int u) {
if (u == parent[u]) {
return u;
}
return parent[u] = get(parent[u]);
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
return;
}
if (rank[a] == rank[b]) {
++rank[a];
}
if (rank[a] < rank[b]) {
swap(a, b);
}
parent[b] = a;
}
} dsu;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<tuple<int, int, int>> edges;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
edges.push_back({w, --u, --v});
}
sort(all(edges));
vector<tuple<int, int, int>> events;
ll bk = -1, bb = get<0>(edges[0]);
for (int i = 0; i < m; ++i) {
dsu.clear();
auto [w, u, v] = edges[i];
events.push_back({2 * w, 2, -2 * w});
for (int j = i - 1; j >= 0; --j) {
auto [wp, up, vp] = edges[j];
dsu.merge(up, vp);
if (dsu.get(u) == dsu.get(v)) {
events.push_back({w + wp, -2, w + wp});
break;
} else if (j == 0) {
--bk;
bb += w;
}
}
}
sort(all(events));
int it = 0;
int queries;
cin >> queries;
while (queries--) {
int x;
cin >> x;
while (it < sz(events) && get<0>(events[it]) <= 2 * x) {
auto [_, dk, db] = events[it++];
bk += dk;
bb += db;
}
cout << ll(bk) * x + bb << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
838 ms |
6436 KB |
Output is correct |
21 |
Correct |
640 ms |
4792 KB |
Output is correct |
22 |
Correct |
678 ms |
4612 KB |
Output is correct |
23 |
Correct |
750 ms |
4676 KB |
Output is correct |
24 |
Correct |
809 ms |
4584 KB |
Output is correct |
25 |
Correct |
800 ms |
4744 KB |
Output is correct |
26 |
Correct |
844 ms |
6368 KB |
Output is correct |
27 |
Correct |
835 ms |
6324 KB |
Output is correct |
28 |
Correct |
826 ms |
6356 KB |
Output is correct |
29 |
Correct |
490 ms |
6516 KB |
Output is correct |
30 |
Correct |
825 ms |
6456 KB |
Output is correct |
31 |
Correct |
830 ms |
6276 KB |
Output is correct |
32 |
Correct |
821 ms |
6264 KB |
Output is correct |
33 |
Correct |
840 ms |
6256 KB |
Output is correct |
34 |
Correct |
361 ms |
6420 KB |
Output is correct |
35 |
Correct |
864 ms |
6516 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 |
931 ms |
26392 KB |
Output is correct |
5 |
Correct |
930 ms |
26284 KB |
Output is correct |
6 |
Correct |
917 ms |
26160 KB |
Output is correct |
7 |
Correct |
587 ms |
28132 KB |
Output is correct |
8 |
Correct |
484 ms |
28324 KB |
Output is correct |
9 |
Correct |
434 ms |
28408 KB |
Output is correct |
10 |
Correct |
900 ms |
26548 KB |
Output is correct |
11 |
Correct |
458 ms |
28516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
230 ms |
21884 KB |
Output is correct |
21 |
Correct |
210 ms |
20020 KB |
Output is correct |
22 |
Correct |
199 ms |
22220 KB |
Output is correct |
23 |
Correct |
205 ms |
22204 KB |
Output is correct |
24 |
Correct |
203 ms |
22288 KB |
Output is correct |
25 |
Correct |
203 ms |
22272 KB |
Output is correct |
26 |
Correct |
201 ms |
22192 KB |
Output is correct |
27 |
Correct |
210 ms |
22208 KB |
Output is correct |
28 |
Correct |
204 ms |
22104 KB |
Output is correct |
29 |
Correct |
200 ms |
22216 KB |
Output is correct |
30 |
Correct |
206 ms |
22348 KB |
Output is correct |
31 |
Correct |
192 ms |
21968 KB |
Output is correct |
32 |
Correct |
203 ms |
22604 KB |
Output is correct |
33 |
Correct |
198 ms |
22032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
838 ms |
6436 KB |
Output is correct |
21 |
Correct |
640 ms |
4792 KB |
Output is correct |
22 |
Correct |
678 ms |
4612 KB |
Output is correct |
23 |
Correct |
750 ms |
4676 KB |
Output is correct |
24 |
Correct |
809 ms |
4584 KB |
Output is correct |
25 |
Correct |
800 ms |
4744 KB |
Output is correct |
26 |
Correct |
844 ms |
6368 KB |
Output is correct |
27 |
Correct |
835 ms |
6324 KB |
Output is correct |
28 |
Correct |
826 ms |
6356 KB |
Output is correct |
29 |
Correct |
490 ms |
6516 KB |
Output is correct |
30 |
Correct |
825 ms |
6456 KB |
Output is correct |
31 |
Correct |
830 ms |
6276 KB |
Output is correct |
32 |
Correct |
821 ms |
6264 KB |
Output is correct |
33 |
Correct |
840 ms |
6256 KB |
Output is correct |
34 |
Correct |
361 ms |
6420 KB |
Output is correct |
35 |
Correct |
864 ms |
6516 KB |
Output is correct |
36 |
Correct |
825 ms |
6596 KB |
Output is correct |
37 |
Correct |
679 ms |
6592 KB |
Output is correct |
38 |
Correct |
683 ms |
6512 KB |
Output is correct |
39 |
Correct |
743 ms |
6540 KB |
Output is correct |
40 |
Correct |
815 ms |
6184 KB |
Output is correct |
41 |
Correct |
804 ms |
6564 KB |
Output is correct |
42 |
Correct |
838 ms |
6336 KB |
Output is correct |
43 |
Correct |
831 ms |
6576 KB |
Output is correct |
44 |
Correct |
835 ms |
6684 KB |
Output is correct |
45 |
Correct |
490 ms |
6476 KB |
Output is correct |
46 |
Correct |
833 ms |
6624 KB |
Output is correct |
47 |
Correct |
831 ms |
6468 KB |
Output is correct |
48 |
Correct |
830 ms |
6484 KB |
Output is correct |
49 |
Correct |
839 ms |
6588 KB |
Output is correct |
50 |
Correct |
361 ms |
6432 KB |
Output is correct |
51 |
Correct |
845 ms |
6332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
838 ms |
6436 KB |
Output is correct |
21 |
Correct |
640 ms |
4792 KB |
Output is correct |
22 |
Correct |
678 ms |
4612 KB |
Output is correct |
23 |
Correct |
750 ms |
4676 KB |
Output is correct |
24 |
Correct |
809 ms |
4584 KB |
Output is correct |
25 |
Correct |
800 ms |
4744 KB |
Output is correct |
26 |
Correct |
844 ms |
6368 KB |
Output is correct |
27 |
Correct |
835 ms |
6324 KB |
Output is correct |
28 |
Correct |
826 ms |
6356 KB |
Output is correct |
29 |
Correct |
490 ms |
6516 KB |
Output is correct |
30 |
Correct |
825 ms |
6456 KB |
Output is correct |
31 |
Correct |
830 ms |
6276 KB |
Output is correct |
32 |
Correct |
821 ms |
6264 KB |
Output is correct |
33 |
Correct |
840 ms |
6256 KB |
Output is correct |
34 |
Correct |
361 ms |
6420 KB |
Output is correct |
35 |
Correct |
864 ms |
6516 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
931 ms |
26392 KB |
Output is correct |
40 |
Correct |
930 ms |
26284 KB |
Output is correct |
41 |
Correct |
917 ms |
26160 KB |
Output is correct |
42 |
Correct |
587 ms |
28132 KB |
Output is correct |
43 |
Correct |
484 ms |
28324 KB |
Output is correct |
44 |
Correct |
434 ms |
28408 KB |
Output is correct |
45 |
Correct |
900 ms |
26548 KB |
Output is correct |
46 |
Correct |
458 ms |
28516 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
230 ms |
21884 KB |
Output is correct |
49 |
Correct |
210 ms |
20020 KB |
Output is correct |
50 |
Correct |
199 ms |
22220 KB |
Output is correct |
51 |
Correct |
205 ms |
22204 KB |
Output is correct |
52 |
Correct |
203 ms |
22288 KB |
Output is correct |
53 |
Correct |
203 ms |
22272 KB |
Output is correct |
54 |
Correct |
201 ms |
22192 KB |
Output is correct |
55 |
Correct |
210 ms |
22208 KB |
Output is correct |
56 |
Correct |
204 ms |
22104 KB |
Output is correct |
57 |
Correct |
200 ms |
22216 KB |
Output is correct |
58 |
Correct |
206 ms |
22348 KB |
Output is correct |
59 |
Correct |
192 ms |
21968 KB |
Output is correct |
60 |
Correct |
203 ms |
22604 KB |
Output is correct |
61 |
Correct |
198 ms |
22032 KB |
Output is correct |
62 |
Correct |
825 ms |
6596 KB |
Output is correct |
63 |
Correct |
679 ms |
6592 KB |
Output is correct |
64 |
Correct |
683 ms |
6512 KB |
Output is correct |
65 |
Correct |
743 ms |
6540 KB |
Output is correct |
66 |
Correct |
815 ms |
6184 KB |
Output is correct |
67 |
Correct |
804 ms |
6564 KB |
Output is correct |
68 |
Correct |
838 ms |
6336 KB |
Output is correct |
69 |
Correct |
831 ms |
6576 KB |
Output is correct |
70 |
Correct |
835 ms |
6684 KB |
Output is correct |
71 |
Correct |
490 ms |
6476 KB |
Output is correct |
72 |
Correct |
833 ms |
6624 KB |
Output is correct |
73 |
Correct |
831 ms |
6468 KB |
Output is correct |
74 |
Correct |
830 ms |
6484 KB |
Output is correct |
75 |
Correct |
839 ms |
6588 KB |
Output is correct |
76 |
Correct |
361 ms |
6432 KB |
Output is correct |
77 |
Correct |
845 ms |
6332 KB |
Output is correct |
78 |
Correct |
1028 ms |
25040 KB |
Output is correct |
79 |
Correct |
851 ms |
24848 KB |
Output is correct |
80 |
Correct |
862 ms |
26288 KB |
Output is correct |
81 |
Correct |
920 ms |
26300 KB |
Output is correct |
82 |
Correct |
1107 ms |
25352 KB |
Output is correct |
83 |
Correct |
1067 ms |
24836 KB |
Output is correct |
84 |
Correct |
1100 ms |
22304 KB |
Output is correct |
85 |
Correct |
1064 ms |
25072 KB |
Output is correct |
86 |
Correct |
983 ms |
14168 KB |
Output is correct |
87 |
Correct |
673 ms |
19768 KB |
Output is correct |
88 |
Correct |
1004 ms |
25124 KB |
Output is correct |
89 |
Correct |
1006 ms |
25072 KB |
Output is correct |
90 |
Correct |
1025 ms |
25156 KB |
Output is correct |
91 |
Correct |
996 ms |
14264 KB |
Output is correct |
92 |
Correct |
534 ms |
16832 KB |
Output is correct |
93 |
Correct |
1002 ms |
15176 KB |
Output is correct |