본문 바로가기
Study/99클럽 코테 스터디

[99클럽 코테 스터디 9일차 TIL] 저울

by hxxyeoniii 2025. 4. 10.

 

9일차 문제

링크 : https://www.acmicpc.net/problem/2437

 

 

 

문제 풀이

문제의 힌트로는 '탐욕법'이라고 나와있었는데, 문제 풀이 개념을 이해하는데까지 시간이 조금 걸렸다.

"누적합 + 1이 다음 값보다 작다면 만들 수 없는 숫자"이다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int[] arr = new int[N];
        st = new StringTokenizer(br.readLine());
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        Arrays.sort(arr);

        int num = 0;
        for(int i=0; i<arr.length; i++) {
           // 누적합 + 1이 다음 값보다 작다면 만들 수 없는 수
           if(num + 1 < arr[i]) {
                break;
            }
            num += arr[i];
        }
        System.out.println(num + 1);
    }
}