아이디어
- 다솜밖에 없는 경우 출력
- 다솜 제외하고, 정렬한후 마지막 인덱스부터 비교하면서, 1표씩 가져오기 (다솜이가 많을때 break문으로 빠져나옴)
import java.util.Arrays;
import java.util.Scanner;
public class Main {
//배열에 저장하자
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n= scan.nextInt();
int som = scan.nextInt();
if(n==1){
System.out.println(0);
return;
}
int[] candi = new int[n-1];
for (int i = 0; i <n-1; i++) {
candi[i] = scan.nextInt(); //1번이 많아야함. 그럼 일단 정렬해보고.
}
int cnt =0;
//비교해서 정렬해야하나
while(true){
//다솜이 1등 될때까지 반복
Arrays.sort(candi);
int maxIdx = n-2; //마지막 인덱스
if (som > candi[maxIdx]){
break;
}
//가장 많이 득표한 후보 1표 가져오기
candi[maxIdx]--;
som++;
cnt++;
}
System.out.println(cnt);
}
}