-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.cpp
48 lines (43 loc) · 1.06 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/5bf67529d3a7434d9c53943021aff0ff
#include<bits/stdc++.h>
using namespace std;
unordered_set<int> st;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, K; cin >> N >> K;
queue<pair<long long, long long>> Q;
vector<long long> V;
for(int i=0;i<N;i++) {
long long x;cin >> x;
V.push_back(x);
st.insert(x);
}
for(auto x: V){
if(st.count(x - 1) == 0) {
Q.emplace(x, x - 1);
st.insert(x - 1);
}
if(st.count(x + 1) == 0) {
Q.emplace(x, x + 1);
st.insert(x + 1);
}
}
long long answer = 0;
while(K--) {
auto [city, cur] = Q.front(); Q.pop();
answer += abs(city - cur);
if(st.count(cur - 1) == 0) {
st.insert(cur - 1);
Q.emplace(city, cur - 1);
}
if(st.count(cur + 1) == 0) {
st.insert(cur + 1);
Q.emplace(city, cur + 1);
}
}
cout << answer;
return 0;
}