|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 샘터 |
| 6 | + * 주의점 |
| 7 | + * 1. 샘터의 위치는 -100_000_000 ~ 100_000_000이지만, 집의 위치는 이걸 벗어날 수 있음 |
| 8 | + * 2. 불행도를 계산할 때, 불행도의 범위 long으로 설정하기 |
| 9 | + * - 1 + 2 + ... + 500,000 = 125,000,250,000 |
| 10 | + */ |
| 11 | + |
| 12 | +public class DH_18513 { |
| 13 | + |
| 14 | + static final int INF = 100_200_000; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 19 | + |
| 20 | + int N = Integer.parseInt(st.nextToken()); // 샘터의 개수 |
| 21 | + int K = Integer.parseInt(st.nextToken()); // 집의 개수 |
| 22 | + |
| 23 | + boolean[] v = new boolean[2 * INF + 1]; // -INF ~ INF |
| 24 | + Queue<int[]> q = new ArrayDeque<int[]>(); |
| 25 | + |
| 26 | + st = new StringTokenizer(br.readLine()); // 샘터에 대한 정보 |
| 27 | + for(int i = 0; i < N; i++) { |
| 28 | + int current = Integer.parseInt(st.nextToken()); |
| 29 | + q.add(new int[] {current, 0}); |
| 30 | + v[current + INF] = true; |
| 31 | + } |
| 32 | + |
| 33 | + int cnt = 0; |
| 34 | + int badCnt = 0; |
| 35 | + |
| 36 | + L: while(!q.isEmpty()) { |
| 37 | + int[] current = q.poll(); |
| 38 | + |
| 39 | + // 좌, 우 확인하기 |
| 40 | + for(int d = -1; d < 2; d += 2) { |
| 41 | + int next = current[0] + d; |
| 42 | + |
| 43 | + if(next < -INF || next > INF || v[next + INF]) continue; |
| 44 | + cnt += 1; |
| 45 | + badCnt += current[1] + 1; |
| 46 | + |
| 47 | + if(cnt ==K) break L; |
| 48 | + |
| 49 | + q.add(new int[] {next, current[1] + 1}); |
| 50 | + v[next + INF] = true; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + System.out.println(badCnt); |
| 55 | + } |
| 56 | +} |
0 commit comments