답안 #796184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796184 2023-07-28T07:34:28 Z GEN 이지후(#10112) Security Guard (JOI23_guard) C++17
76 / 100
340 ms 321132 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<int, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXN = 200005;

// case tree:
// root with maximum
// anchor in parent
// using the guard from root, can do everything

struct disj {
	int pa[MAXN];
	void init(int n) { iota(pa, pa + n + 1, 0); }
	int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); }
	bool uni(int p, int q) {
		p = find(p);
		q = find(q);
		if (p == q)
			return 0;
		pa[q] = p;
		return 1;
	}
} disj;

vector<pi> gph[MAXN];
const int MAXC = 3005;
lint a[MAXN];
lint dp[MAXC][MAXC][2], aux[MAXC][2]; // one for each sub
int sz[MAXN];

void dfs(int x, int p) {
	sz[x] = 1;
	dp[x][0][0] = 0;
	dp[x][0][1] = a[x];
	for (auto &[w, y] : gph[x]) {
		if (y == p)
			continue;
		dfs(y, x);
		memset(aux, 0x3f, sizeof(aux));
		for (int i = 0; i < sz[y]; i++) {
			dp[y][i + 1][0] = min(dp[y][i + 1][0], dp[y][i][1] - w);
		}
		for (int i = 0; i <= sz[x]; i++) {
			for (int j = 0; j <= sz[y]; j++) {
				for (int k = 0; k < 2; k++) {
					for (int l = 0; k + l < 2; l++) {
						aux[i + j][k + l] = min(aux[i + j][k + l], dp[x][i][k] + dp[y][j][l]);
					}
				}
			}
		}
		sz[x] += sz[y];
		for (int i = 0; i <= sz[x]; i++) {
			dp[x][i][0] = aux[i][0];
			dp[x][i][1] = aux[i][1];
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<array<int, 3>> edges(m);
	for (auto &[x, s, e] : edges) {
		cin >> s >> e;
		s--;
		e--;
		x = a[s] + a[e];
	}
	sort(all(edges));
	lint ans = -accumulate(a, a + n, 0ll) + *max_element(a, a + n);
	disj.init(n);
	for (auto &[x, s, e] : edges) {
		if (disj.uni(s, e)) {
			ans += x;
			gph[s].push_back({x, e});
			gph[e].push_back({x, s});
		}
	}
	cout << ans << "\n";
	if (q > 0) {
		int rt = min_element(a, a + n) - a;
		memset(dp, 0x3f, sizeof(dp));
		dfs(rt, -1);
		for (int i = 1; i <= q; i++) {
			cout << ans + a[rt] * min(i, n - 1) + dp[rt][min(i, n - 1)][0] << "\n";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 60 ms 15864 KB Output is correct
3 Correct 60 ms 15956 KB Output is correct
4 Correct 68 ms 15856 KB Output is correct
5 Correct 68 ms 15852 KB Output is correct
6 Correct 80 ms 15848 KB Output is correct
7 Correct 78 ms 15852 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 60 ms 15864 KB Output is correct
3 Correct 60 ms 15956 KB Output is correct
4 Correct 68 ms 15856 KB Output is correct
5 Correct 68 ms 15852 KB Output is correct
6 Correct 80 ms 15848 KB Output is correct
7 Correct 78 ms 15852 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 83 ms 15908 KB Output is correct
11 Correct 83 ms 15948 KB Output is correct
12 Correct 63 ms 15848 KB Output is correct
13 Correct 63 ms 15852 KB Output is correct
14 Correct 104 ms 15880 KB Output is correct
15 Correct 86 ms 15880 KB Output is correct
16 Correct 86 ms 15864 KB Output is correct
17 Correct 89 ms 15856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 60 ms 15864 KB Output is correct
3 Correct 60 ms 15956 KB Output is correct
4 Correct 68 ms 15856 KB Output is correct
5 Correct 68 ms 15852 KB Output is correct
6 Correct 80 ms 15848 KB Output is correct
7 Correct 78 ms 15852 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 83 ms 15908 KB Output is correct
11 Correct 83 ms 15948 KB Output is correct
12 Correct 63 ms 15848 KB Output is correct
13 Correct 63 ms 15852 KB Output is correct
14 Correct 104 ms 15880 KB Output is correct
15 Correct 86 ms 15880 KB Output is correct
16 Correct 86 ms 15864 KB Output is correct
17 Correct 89 ms 15856 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 92 ms 16756 KB Output is correct
20 Correct 143 ms 16824 KB Output is correct
21 Correct 96 ms 16876 KB Output is correct
22 Correct 97 ms 17128 KB Output is correct
23 Correct 84 ms 17744 KB Output is correct
24 Correct 84 ms 17656 KB Output is correct
25 Correct 93 ms 17508 KB Output is correct
26 Correct 81 ms 17500 KB Output is correct
27 Correct 94 ms 17444 KB Output is correct
28 Correct 120 ms 16324 KB Output is correct
29 Correct 105 ms 16608 KB Output is correct
30 Correct 99 ms 17292 KB Output is correct
31 Correct 84 ms 17428 KB Output is correct
32 Correct 96 ms 17508 KB Output is correct
33 Correct 122 ms 18288 KB Output is correct
34 Correct 113 ms 17048 KB Output is correct
35 Correct 100 ms 18152 KB Output is correct
36 Correct 90 ms 18008 KB Output is correct
37 Correct 104 ms 17948 KB Output is correct
38 Correct 98 ms 17760 KB Output is correct
39 Correct 82 ms 17540 KB Output is correct
40 Correct 108 ms 17920 KB Output is correct
41 Correct 93 ms 15856 KB Output is correct
42 Correct 90 ms 15848 KB Output is correct
43 Correct 103 ms 17128 KB Output is correct
44 Correct 120 ms 17152 KB Output is correct
45 Correct 129 ms 17084 KB Output is correct
46 Correct 120 ms 17116 KB Output is correct
47 Correct 110 ms 17188 KB Output is correct
48 Correct 137 ms 17132 KB Output is correct
49 Correct 95 ms 17100 KB Output is correct
50 Correct 104 ms 17160 KB Output is correct
51 Correct 111 ms 17132 KB Output is correct
52 Correct 120 ms 17144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 60 ms 15864 KB Output is correct
3 Correct 60 ms 15956 KB Output is correct
4 Correct 68 ms 15856 KB Output is correct
5 Correct 68 ms 15852 KB Output is correct
6 Correct 80 ms 15848 KB Output is correct
7 Correct 78 ms 15852 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 83 ms 15908 KB Output is correct
11 Correct 83 ms 15948 KB Output is correct
12 Correct 63 ms 15848 KB Output is correct
13 Correct 63 ms 15852 KB Output is correct
14 Correct 104 ms 15880 KB Output is correct
15 Correct 86 ms 15880 KB Output is correct
16 Correct 86 ms 15864 KB Output is correct
17 Correct 89 ms 15856 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 92 ms 16756 KB Output is correct
20 Correct 143 ms 16824 KB Output is correct
21 Correct 96 ms 16876 KB Output is correct
22 Correct 97 ms 17128 KB Output is correct
23 Correct 84 ms 17744 KB Output is correct
24 Correct 84 ms 17656 KB Output is correct
25 Correct 93 ms 17508 KB Output is correct
26 Correct 81 ms 17500 KB Output is correct
27 Correct 94 ms 17444 KB Output is correct
28 Correct 120 ms 16324 KB Output is correct
29 Correct 105 ms 16608 KB Output is correct
30 Correct 99 ms 17292 KB Output is correct
31 Correct 84 ms 17428 KB Output is correct
32 Correct 96 ms 17508 KB Output is correct
33 Correct 122 ms 18288 KB Output is correct
34 Correct 113 ms 17048 KB Output is correct
35 Correct 100 ms 18152 KB Output is correct
36 Correct 90 ms 18008 KB Output is correct
37 Correct 104 ms 17948 KB Output is correct
38 Correct 98 ms 17760 KB Output is correct
39 Correct 82 ms 17540 KB Output is correct
40 Correct 108 ms 17920 KB Output is correct
41 Correct 93 ms 15856 KB Output is correct
42 Correct 90 ms 15848 KB Output is correct
43 Correct 103 ms 17128 KB Output is correct
44 Correct 120 ms 17152 KB Output is correct
45 Correct 129 ms 17084 KB Output is correct
46 Correct 120 ms 17116 KB Output is correct
47 Correct 110 ms 17188 KB Output is correct
48 Correct 137 ms 17132 KB Output is correct
49 Correct 95 ms 17100 KB Output is correct
50 Correct 104 ms 17160 KB Output is correct
51 Correct 111 ms 17132 KB Output is correct
52 Correct 120 ms 17144 KB Output is correct
53 Correct 2 ms 4948 KB Output is correct
54 Correct 103 ms 16868 KB Output is correct
55 Correct 106 ms 16880 KB Output is correct
56 Correct 110 ms 17612 KB Output is correct
57 Correct 167 ms 19484 KB Output is correct
58 Correct 149 ms 20108 KB Output is correct
59 Correct 144 ms 20000 KB Output is correct
60 Correct 114 ms 18628 KB Output is correct
61 Correct 142 ms 19852 KB Output is correct
62 Correct 152 ms 19868 KB Output is correct
63 Correct 96 ms 16364 KB Output is correct
64 Correct 131 ms 17768 KB Output is correct
65 Correct 144 ms 19648 KB Output is correct
66 Correct 136 ms 19848 KB Output is correct
67 Correct 157 ms 19792 KB Output is correct
68 Correct 98 ms 18324 KB Output is correct
69 Correct 162 ms 18476 KB Output is correct
70 Correct 109 ms 18448 KB Output is correct
71 Correct 92 ms 18028 KB Output is correct
72 Correct 91 ms 17996 KB Output is correct
73 Correct 129 ms 17740 KB Output is correct
74 Correct 85 ms 17508 KB Output is correct
75 Correct 109 ms 17924 KB Output is correct
76 Correct 96 ms 15868 KB Output is correct
77 Correct 91 ms 15984 KB Output is correct
78 Correct 155 ms 17280 KB Output is correct
79 Correct 167 ms 19480 KB Output is correct
80 Correct 129 ms 17800 KB Output is correct
81 Correct 168 ms 19008 KB Output is correct
82 Correct 156 ms 19484 KB Output is correct
83 Correct 168 ms 19432 KB Output is correct
84 Correct 166 ms 19528 KB Output is correct
85 Correct 98 ms 17140 KB Output is correct
86 Correct 108 ms 17168 KB Output is correct
87 Correct 98 ms 17124 KB Output is correct
88 Correct 199 ms 19540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 52 ms 146420 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 56 ms 146360 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 60 ms 148632 KB Output is correct
9 Correct 62 ms 148532 KB Output is correct
10 Correct 62 ms 148392 KB Output is correct
11 Correct 68 ms 148640 KB Output is correct
12 Correct 59 ms 148528 KB Output is correct
13 Correct 59 ms 148532 KB Output is correct
14 Correct 59 ms 148624 KB Output is correct
15 Correct 73 ms 148596 KB Output is correct
16 Correct 63 ms 148620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 52 ms 146420 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 56 ms 146360 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 60 ms 148632 KB Output is correct
9 Correct 62 ms 148532 KB Output is correct
10 Correct 62 ms 148392 KB Output is correct
11 Correct 68 ms 148640 KB Output is correct
12 Correct 59 ms 148528 KB Output is correct
13 Correct 59 ms 148532 KB Output is correct
14 Correct 59 ms 148624 KB Output is correct
15 Correct 73 ms 148596 KB Output is correct
16 Correct 63 ms 148620 KB Output is correct
17 Correct 89 ms 149312 KB Output is correct
18 Correct 87 ms 149228 KB Output is correct
19 Correct 98 ms 149852 KB Output is correct
20 Correct 113 ms 150944 KB Output is correct
21 Correct 128 ms 151652 KB Output is correct
22 Correct 138 ms 151100 KB Output is correct
23 Correct 192 ms 153760 KB Output is correct
24 Correct 153 ms 151764 KB Output is correct
25 Correct 74 ms 148852 KB Output is correct
26 Correct 112 ms 150348 KB Output is correct
27 Correct 81 ms 149208 KB Output is correct
28 Correct 99 ms 148936 KB Output is correct
29 Correct 82 ms 148968 KB Output is correct
30 Correct 84 ms 148972 KB Output is correct
31 Correct 89 ms 147916 KB Output is correct
32 Correct 95 ms 147868 KB Output is correct
33 Correct 75 ms 148812 KB Output is correct
34 Correct 76 ms 148904 KB Output is correct
35 Correct 96 ms 149964 KB Output is correct
36 Correct 188 ms 153776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 60 ms 15864 KB Output is correct
3 Correct 60 ms 15956 KB Output is correct
4 Correct 68 ms 15856 KB Output is correct
5 Correct 68 ms 15852 KB Output is correct
6 Correct 80 ms 15848 KB Output is correct
7 Correct 78 ms 15852 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 83 ms 15908 KB Output is correct
11 Correct 83 ms 15948 KB Output is correct
12 Correct 63 ms 15848 KB Output is correct
13 Correct 63 ms 15852 KB Output is correct
14 Correct 104 ms 15880 KB Output is correct
15 Correct 86 ms 15880 KB Output is correct
16 Correct 86 ms 15864 KB Output is correct
17 Correct 89 ms 15856 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 92 ms 16756 KB Output is correct
20 Correct 143 ms 16824 KB Output is correct
21 Correct 96 ms 16876 KB Output is correct
22 Correct 97 ms 17128 KB Output is correct
23 Correct 84 ms 17744 KB Output is correct
24 Correct 84 ms 17656 KB Output is correct
25 Correct 93 ms 17508 KB Output is correct
26 Correct 81 ms 17500 KB Output is correct
27 Correct 94 ms 17444 KB Output is correct
28 Correct 120 ms 16324 KB Output is correct
29 Correct 105 ms 16608 KB Output is correct
30 Correct 99 ms 17292 KB Output is correct
31 Correct 84 ms 17428 KB Output is correct
32 Correct 96 ms 17508 KB Output is correct
33 Correct 122 ms 18288 KB Output is correct
34 Correct 113 ms 17048 KB Output is correct
35 Correct 100 ms 18152 KB Output is correct
36 Correct 90 ms 18008 KB Output is correct
37 Correct 104 ms 17948 KB Output is correct
38 Correct 98 ms 17760 KB Output is correct
39 Correct 82 ms 17540 KB Output is correct
40 Correct 108 ms 17920 KB Output is correct
41 Correct 93 ms 15856 KB Output is correct
42 Correct 90 ms 15848 KB Output is correct
43 Correct 103 ms 17128 KB Output is correct
44 Correct 120 ms 17152 KB Output is correct
45 Correct 129 ms 17084 KB Output is correct
46 Correct 120 ms 17116 KB Output is correct
47 Correct 110 ms 17188 KB Output is correct
48 Correct 137 ms 17132 KB Output is correct
49 Correct 95 ms 17100 KB Output is correct
50 Correct 104 ms 17160 KB Output is correct
51 Correct 111 ms 17132 KB Output is correct
52 Correct 120 ms 17144 KB Output is correct
53 Correct 2 ms 4948 KB Output is correct
54 Correct 103 ms 16868 KB Output is correct
55 Correct 106 ms 16880 KB Output is correct
56 Correct 110 ms 17612 KB Output is correct
57 Correct 167 ms 19484 KB Output is correct
58 Correct 149 ms 20108 KB Output is correct
59 Correct 144 ms 20000 KB Output is correct
60 Correct 114 ms 18628 KB Output is correct
61 Correct 142 ms 19852 KB Output is correct
62 Correct 152 ms 19868 KB Output is correct
63 Correct 96 ms 16364 KB Output is correct
64 Correct 131 ms 17768 KB Output is correct
65 Correct 144 ms 19648 KB Output is correct
66 Correct 136 ms 19848 KB Output is correct
67 Correct 157 ms 19792 KB Output is correct
68 Correct 98 ms 18324 KB Output is correct
69 Correct 162 ms 18476 KB Output is correct
70 Correct 109 ms 18448 KB Output is correct
71 Correct 92 ms 18028 KB Output is correct
72 Correct 91 ms 17996 KB Output is correct
73 Correct 129 ms 17740 KB Output is correct
74 Correct 85 ms 17508 KB Output is correct
75 Correct 109 ms 17924 KB Output is correct
76 Correct 96 ms 15868 KB Output is correct
77 Correct 91 ms 15984 KB Output is correct
78 Correct 155 ms 17280 KB Output is correct
79 Correct 167 ms 19480 KB Output is correct
80 Correct 129 ms 17800 KB Output is correct
81 Correct 168 ms 19008 KB Output is correct
82 Correct 156 ms 19484 KB Output is correct
83 Correct 168 ms 19432 KB Output is correct
84 Correct 166 ms 19528 KB Output is correct
85 Correct 98 ms 17140 KB Output is correct
86 Correct 108 ms 17168 KB Output is correct
87 Correct 98 ms 17124 KB Output is correct
88 Correct 199 ms 19540 KB Output is correct
89 Correct 3 ms 4948 KB Output is correct
90 Correct 52 ms 146420 KB Output is correct
91 Correct 3 ms 4948 KB Output is correct
92 Correct 3 ms 4948 KB Output is correct
93 Correct 2 ms 4948 KB Output is correct
94 Correct 56 ms 146360 KB Output is correct
95 Correct 2 ms 4948 KB Output is correct
96 Correct 60 ms 148632 KB Output is correct
97 Correct 62 ms 148532 KB Output is correct
98 Correct 62 ms 148392 KB Output is correct
99 Correct 68 ms 148640 KB Output is correct
100 Correct 59 ms 148528 KB Output is correct
101 Correct 59 ms 148532 KB Output is correct
102 Correct 59 ms 148624 KB Output is correct
103 Correct 73 ms 148596 KB Output is correct
104 Correct 63 ms 148620 KB Output is correct
105 Correct 89 ms 149312 KB Output is correct
106 Correct 87 ms 149228 KB Output is correct
107 Correct 98 ms 149852 KB Output is correct
108 Correct 113 ms 150944 KB Output is correct
109 Correct 128 ms 151652 KB Output is correct
110 Correct 138 ms 151100 KB Output is correct
111 Correct 192 ms 153760 KB Output is correct
112 Correct 153 ms 151764 KB Output is correct
113 Correct 74 ms 148852 KB Output is correct
114 Correct 112 ms 150348 KB Output is correct
115 Correct 81 ms 149208 KB Output is correct
116 Correct 99 ms 148936 KB Output is correct
117 Correct 82 ms 148968 KB Output is correct
118 Correct 84 ms 148972 KB Output is correct
119 Correct 89 ms 147916 KB Output is correct
120 Correct 95 ms 147868 KB Output is correct
121 Correct 75 ms 148812 KB Output is correct
122 Correct 76 ms 148904 KB Output is correct
123 Correct 96 ms 149964 KB Output is correct
124 Correct 188 ms 153776 KB Output is correct
125 Runtime error 340 ms 321132 KB Execution killed with signal 11
126 Halted 0 ms 0 KB -