개발자 톡
연습문제 톡
금고털이
TC 2, 5번 안되는 이유 알려주세요ㅠ
- 등록일
- 2024-07-22 00:39:14
- 조회수
- 220
- 작성자
- areumj0507
import java.io.*; import java.util.*; public class Main { private static List<Map.Entry<Integer, Integer>> sortPriceAndWeight(Map<Integer, Integer> metalMap) { List<Map.Entry<Integer, Integer>> entryList = new LinkedList<>(metalMap.entrySet()); entryList.sort(new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return o2.getValue() - o1.getValue(); } }); return entryList; } private static int sumPrice(int W, List<Map.Entry<Integer, Integer>> entryList) { int result = 0; for(Map.Entry<Integer, Integer> entry : entryList) { if(W == 0) break; int weight = entry.getKey(); int price = entry.getValue(); if(W >= weight) { result += (weight * price); W -= weight; }else { result += (W * price); W = 0; } } return result; } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(reader.readLine()); int W = Integer.parseInt(tokenizer.nextToken()); int N = Integer.parseInt(tokenizer.nextToken()); Map<Integer, Integer> metalMap = new HashMap<>(); for(int i=0; i<N; i++) { tokenizer = new StringTokenizer(reader.readLine()); metalMap.put(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken())); } System.out.println((sumPrice(W, sortPriceAndWeight(metalMap)))); } }
안되는 이유 좀 알려주세요ㅠㅠ
#금고털이