본문 바로가기
백준/1 - 5000

[백준] 2785번 : 체인

by lms0806 2024. 11. 9.
728x90
반응형

https://www.acmicpc.net/problem/2785

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		
		ArrayList<Integer> arr = new ArrayList<>();
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i = 0; i < n; i++) {
			arr.add(Integer.parseInt(st.nextToken()));
		}
		
		Collections.sort(arr);
		
		int answer = 0;
		while(true) {
			if(arr.size() < 2) {
				break;
			}
			
			arr.set(0, arr.get(0) - 1);
			arr.remove(arr.size() - 1);
			
			if(arr.get(0) == 0) {
				arr.remove(0);
			}
			
			answer++;
		}
		System.out.print(answer);
	}
}
728x90
반응형

댓글