Submission #1030910

# Submission time Handle Problem Language Result Execution time Memory
1030910 2024-07-22T11:51:03 Z Unforgettablepl Security Guard (JOI23_guard) C++17
50 / 100
225 ms 28848 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

struct UFDS{
	vector<int> p,siz,mini;
	multiset<int> miniset;
	int sigma,compo;
	UFDS(int n,vector<int> arr):p(n+1),siz(n+1,1),mini(n+1),sigma(0),compo(n){
		iota(p.begin(),p.end(),0);
		mini=arr;
		for(int i=1;i<=n;i++){
			miniset.insert(arr[i]);
			sigma+=arr[i];
		}
	}
	int findRoot(int x){
		return p[x]==x ? x : p[x]=findRoot(p[x]);
	}
	int getans(){
		return sigma+(compo-2ll)*(*miniset.begin());
	}
	bool unite(int a,int b){
		a = findRoot(a);
		b = findRoot(b);
		if(a==b)return false;
		if(siz[b]>siz[a])swap(a,b);
		int maxi = max(mini[a],mini[b]);
		mini[a]=min(mini[a],mini[b]);
		miniset.erase(miniset.find(maxi));
		sigma-=maxi;
		siz[a]+=siz[b];
		p[b]=a;
		compo--;
		return true;
	}
};


int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,m,q;
	cin >> n >> m >> q;
	vector<int> arr(n+1);
	for(int i=1;i<=n;i++)cin>>arr[i];
	vector<tuple<int,int,int>> edges;
	for(int i=1;i<=m;i++){
		int a,b;
		cin >> a >> b;
		edges.emplace_back(arr[a]+arr[b],a,b);
	}
	sort(edges.begin(),edges.end());
	int baseans = (*max_element(arr.begin(),arr.end()));
	for(int i=1;i<=n;i++){
		baseans-=arr[i];
	}
	UFDS dsu(n,arr);
	vector<int> ans(n);
	ans[0] = dsu.getans();
	int iter = 1;
	int offset = 0;
	for(int i=0;i<m;i++){
		auto [cost,a,b] = edges[i];
		if(dsu.unite(a,b)){
			offset+=cost;
			ans[iter++]=offset+dsu.getans();
		}
	}
	for(int i=0;i<=q;i++){
		cout << baseans+ans[max(0ll,n-1ll-i)] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 78 ms 22340 KB Output is correct
3 Correct 80 ms 22964 KB Output is correct
4 Correct 79 ms 22444 KB Output is correct
5 Correct 79 ms 22216 KB Output is correct
6 Correct 77 ms 22560 KB Output is correct
7 Correct 79 ms 22224 KB Output is correct
8 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 78 ms 22340 KB Output is correct
3 Correct 80 ms 22964 KB Output is correct
4 Correct 79 ms 22444 KB Output is correct
5 Correct 79 ms 22216 KB Output is correct
6 Correct 77 ms 22560 KB Output is correct
7 Correct 79 ms 22224 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 173 ms 23116 KB Output is correct
11 Correct 85 ms 22304 KB Output is correct
12 Correct 83 ms 23224 KB Output is correct
13 Correct 84 ms 22712 KB Output is correct
14 Correct 176 ms 22360 KB Output is correct
15 Correct 173 ms 22204 KB Output is correct
16 Correct 168 ms 22308 KB Output is correct
17 Correct 161 ms 22964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 78 ms 22340 KB Output is correct
3 Correct 80 ms 22964 KB Output is correct
4 Correct 79 ms 22444 KB Output is correct
5 Correct 79 ms 22216 KB Output is correct
6 Correct 77 ms 22560 KB Output is correct
7 Correct 79 ms 22224 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 173 ms 23116 KB Output is correct
11 Correct 85 ms 22304 KB Output is correct
12 Correct 83 ms 23224 KB Output is correct
13 Correct 84 ms 22712 KB Output is correct
14 Correct 176 ms 22360 KB Output is correct
15 Correct 173 ms 22204 KB Output is correct
16 Correct 168 ms 22308 KB Output is correct
17 Correct 161 ms 22964 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 179 ms 23648 KB Output is correct
20 Correct 183 ms 22204 KB Output is correct
21 Correct 215 ms 22708 KB Output is correct
22 Correct 179 ms 22196 KB Output is correct
23 Correct 163 ms 22200 KB Output is correct
24 Correct 165 ms 22460 KB Output is correct
25 Correct 135 ms 22460 KB Output is correct
26 Correct 128 ms 23416 KB Output is correct
27 Correct 119 ms 22204 KB Output is correct
28 Correct 178 ms 22708 KB Output is correct
29 Correct 195 ms 22812 KB Output is correct
30 Correct 140 ms 22460 KB Output is correct
31 Correct 123 ms 23480 KB Output is correct
32 Correct 175 ms 22456 KB Output is correct
33 Correct 114 ms 22200 KB Output is correct
34 Correct 101 ms 23480 KB Output is correct
35 Correct 98 ms 22328 KB Output is correct
36 Correct 93 ms 22968 KB Output is correct
37 Correct 90 ms 22204 KB Output is correct
38 Correct 124 ms 22200 KB Output is correct
39 Correct 107 ms 23224 KB Output is correct
40 Correct 109 ms 23112 KB Output is correct
41 Correct 118 ms 22968 KB Output is correct
42 Correct 116 ms 22456 KB Output is correct
43 Correct 96 ms 23480 KB Output is correct
44 Correct 173 ms 22968 KB Output is correct
45 Correct 101 ms 22204 KB Output is correct
46 Correct 115 ms 23740 KB Output is correct
47 Correct 134 ms 22204 KB Output is correct
48 Correct 192 ms 22548 KB Output is correct
49 Correct 125 ms 23736 KB Output is correct
50 Correct 149 ms 22200 KB Output is correct
51 Correct 107 ms 22200 KB Output is correct
52 Correct 104 ms 22460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 78 ms 22340 KB Output is correct
3 Correct 80 ms 22964 KB Output is correct
4 Correct 79 ms 22444 KB Output is correct
5 Correct 79 ms 22216 KB Output is correct
6 Correct 77 ms 22560 KB Output is correct
7 Correct 79 ms 22224 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 173 ms 23116 KB Output is correct
11 Correct 85 ms 22304 KB Output is correct
12 Correct 83 ms 23224 KB Output is correct
13 Correct 84 ms 22712 KB Output is correct
14 Correct 176 ms 22360 KB Output is correct
15 Correct 173 ms 22204 KB Output is correct
16 Correct 168 ms 22308 KB Output is correct
17 Correct 161 ms 22964 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 179 ms 23648 KB Output is correct
20 Correct 183 ms 22204 KB Output is correct
21 Correct 215 ms 22708 KB Output is correct
22 Correct 179 ms 22196 KB Output is correct
23 Correct 163 ms 22200 KB Output is correct
24 Correct 165 ms 22460 KB Output is correct
25 Correct 135 ms 22460 KB Output is correct
26 Correct 128 ms 23416 KB Output is correct
27 Correct 119 ms 22204 KB Output is correct
28 Correct 178 ms 22708 KB Output is correct
29 Correct 195 ms 22812 KB Output is correct
30 Correct 140 ms 22460 KB Output is correct
31 Correct 123 ms 23480 KB Output is correct
32 Correct 175 ms 22456 KB Output is correct
33 Correct 114 ms 22200 KB Output is correct
34 Correct 101 ms 23480 KB Output is correct
35 Correct 98 ms 22328 KB Output is correct
36 Correct 93 ms 22968 KB Output is correct
37 Correct 90 ms 22204 KB Output is correct
38 Correct 124 ms 22200 KB Output is correct
39 Correct 107 ms 23224 KB Output is correct
40 Correct 109 ms 23112 KB Output is correct
41 Correct 118 ms 22968 KB Output is correct
42 Correct 116 ms 22456 KB Output is correct
43 Correct 96 ms 23480 KB Output is correct
44 Correct 173 ms 22968 KB Output is correct
45 Correct 101 ms 22204 KB Output is correct
46 Correct 115 ms 23740 KB Output is correct
47 Correct 134 ms 22204 KB Output is correct
48 Correct 192 ms 22548 KB Output is correct
49 Correct 125 ms 23736 KB Output is correct
50 Correct 149 ms 22200 KB Output is correct
51 Correct 107 ms 22200 KB Output is correct
52 Correct 104 ms 22460 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 176 ms 23528 KB Output is correct
55 Correct 179 ms 22456 KB Output is correct
56 Correct 199 ms 25516 KB Output is correct
57 Correct 222 ms 28332 KB Output is correct
58 Correct 214 ms 27212 KB Output is correct
59 Correct 186 ms 28848 KB Output is correct
60 Correct 153 ms 24748 KB Output is correct
61 Correct 165 ms 27728 KB Output is correct
62 Correct 168 ms 27568 KB Output is correct
63 Correct 200 ms 22460 KB Output is correct
64 Correct 174 ms 25116 KB Output is correct
65 Correct 181 ms 27828 KB Output is correct
66 Correct 160 ms 28548 KB Output is correct
67 Correct 203 ms 26932 KB Output is correct
68 Correct 105 ms 23480 KB Output is correct
69 Correct 134 ms 26616 KB Output is correct
70 Correct 101 ms 22712 KB Output is correct
71 Correct 88 ms 22196 KB Output is correct
72 Correct 89 ms 23224 KB Output is correct
73 Correct 129 ms 23396 KB Output is correct
74 Correct 106 ms 22460 KB Output is correct
75 Correct 114 ms 22696 KB Output is correct
76 Correct 118 ms 23740 KB Output is correct
77 Correct 138 ms 22316 KB Output is correct
78 Correct 100 ms 22712 KB Output is correct
79 Correct 205 ms 28596 KB Output is correct
80 Correct 122 ms 23796 KB Output is correct
81 Correct 152 ms 26288 KB Output is correct
82 Correct 184 ms 27824 KB Output is correct
83 Correct 225 ms 27444 KB Output is correct
84 Correct 157 ms 28340 KB Output is correct
85 Correct 136 ms 22200 KB Output is correct
86 Correct 105 ms 22200 KB Output is correct
87 Correct 97 ms 22712 KB Output is correct
88 Correct 212 ms 28084 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 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 348 KB Output is correct
6 Incorrect 0 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 78 ms 22340 KB Output is correct
3 Correct 80 ms 22964 KB Output is correct
4 Correct 79 ms 22444 KB Output is correct
5 Correct 79 ms 22216 KB Output is correct
6 Correct 77 ms 22560 KB Output is correct
7 Correct 79 ms 22224 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 173 ms 23116 KB Output is correct
11 Correct 85 ms 22304 KB Output is correct
12 Correct 83 ms 23224 KB Output is correct
13 Correct 84 ms 22712 KB Output is correct
14 Correct 176 ms 22360 KB Output is correct
15 Correct 173 ms 22204 KB Output is correct
16 Correct 168 ms 22308 KB Output is correct
17 Correct 161 ms 22964 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 179 ms 23648 KB Output is correct
20 Correct 183 ms 22204 KB Output is correct
21 Correct 215 ms 22708 KB Output is correct
22 Correct 179 ms 22196 KB Output is correct
23 Correct 163 ms 22200 KB Output is correct
24 Correct 165 ms 22460 KB Output is correct
25 Correct 135 ms 22460 KB Output is correct
26 Correct 128 ms 23416 KB Output is correct
27 Correct 119 ms 22204 KB Output is correct
28 Correct 178 ms 22708 KB Output is correct
29 Correct 195 ms 22812 KB Output is correct
30 Correct 140 ms 22460 KB Output is correct
31 Correct 123 ms 23480 KB Output is correct
32 Correct 175 ms 22456 KB Output is correct
33 Correct 114 ms 22200 KB Output is correct
34 Correct 101 ms 23480 KB Output is correct
35 Correct 98 ms 22328 KB Output is correct
36 Correct 93 ms 22968 KB Output is correct
37 Correct 90 ms 22204 KB Output is correct
38 Correct 124 ms 22200 KB Output is correct
39 Correct 107 ms 23224 KB Output is correct
40 Correct 109 ms 23112 KB Output is correct
41 Correct 118 ms 22968 KB Output is correct
42 Correct 116 ms 22456 KB Output is correct
43 Correct 96 ms 23480 KB Output is correct
44 Correct 173 ms 22968 KB Output is correct
45 Correct 101 ms 22204 KB Output is correct
46 Correct 115 ms 23740 KB Output is correct
47 Correct 134 ms 22204 KB Output is correct
48 Correct 192 ms 22548 KB Output is correct
49 Correct 125 ms 23736 KB Output is correct
50 Correct 149 ms 22200 KB Output is correct
51 Correct 107 ms 22200 KB Output is correct
52 Correct 104 ms 22460 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 176 ms 23528 KB Output is correct
55 Correct 179 ms 22456 KB Output is correct
56 Correct 199 ms 25516 KB Output is correct
57 Correct 222 ms 28332 KB Output is correct
58 Correct 214 ms 27212 KB Output is correct
59 Correct 186 ms 28848 KB Output is correct
60 Correct 153 ms 24748 KB Output is correct
61 Correct 165 ms 27728 KB Output is correct
62 Correct 168 ms 27568 KB Output is correct
63 Correct 200 ms 22460 KB Output is correct
64 Correct 174 ms 25116 KB Output is correct
65 Correct 181 ms 27828 KB Output is correct
66 Correct 160 ms 28548 KB Output is correct
67 Correct 203 ms 26932 KB Output is correct
68 Correct 105 ms 23480 KB Output is correct
69 Correct 134 ms 26616 KB Output is correct
70 Correct 101 ms 22712 KB Output is correct
71 Correct 88 ms 22196 KB Output is correct
72 Correct 89 ms 23224 KB Output is correct
73 Correct 129 ms 23396 KB Output is correct
74 Correct 106 ms 22460 KB Output is correct
75 Correct 114 ms 22696 KB Output is correct
76 Correct 118 ms 23740 KB Output is correct
77 Correct 138 ms 22316 KB Output is correct
78 Correct 100 ms 22712 KB Output is correct
79 Correct 205 ms 28596 KB Output is correct
80 Correct 122 ms 23796 KB Output is correct
81 Correct 152 ms 26288 KB Output is correct
82 Correct 184 ms 27824 KB Output is correct
83 Correct 225 ms 27444 KB Output is correct
84 Correct 157 ms 28340 KB Output is correct
85 Correct 136 ms 22200 KB Output is correct
86 Correct 105 ms 22200 KB Output is correct
87 Correct 97 ms 22712 KB Output is correct
88 Correct 212 ms 28084 KB Output is correct
89 Correct 0 ms 344 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 0 ms 348 KB Output is correct
93 Correct 0 ms 348 KB Output is correct
94 Incorrect 0 ms 348 KB Output isn't correct
95 Halted 0 ms 0 KB -