개발자 톡

연습문제 톡 금고털이

Tc5만 틀리는데 이유가 뭘까요... 도저히 모르겠네요 ( Java )

등록일
2024-09-28 19:39:14
조회수
137
작성자
youngyou1324
public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int w = Integer.parseInt(st.nextToken());
    int b = Integer.parseInt(st.nextToken());

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

    Arrays.sort(arr, (x, y)-> {
        if(x[1] == y[1]){
            return y[0] - x[0];
        }
        return y[1] - x[1];
    });

    int answer =0;
    int left=w;

    if(left == arr[0][0]){
        System.out.println(arr[0][0]*arr[0][1]);
        return;
    }

    for(int i=0;i<b;i++){
        if(left>arr[i][0]){
            answer+=(arr[i][0]*arr[i][1]);
            left= (left -  arr[i][0]);
        }else if(left< arr[i][0]){
            answer+=left*arr[i][1];
            break;
        }
    }

    System.out.println(answer);
}


#금고털이

이 카테고리의 톡 더보기