[BaekJoon] 1106 호텔 (Java)

🔗 문제 링크

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


👨🏻‍💻 작성한 코드

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

public class Main {

	static int c, n; 
	static int[] values; 
	public static void main(String[] args) throws IOException { 
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		StringTokenizer st = new StringTokenizer(br.readLine()); 
		c = Integer.parseInt(st.nextToken()); values = new int[c+101]; 
		Arrays.fill(values, Integer.MAX_VALUE); 
		n = Integer.parseInt(st.nextToken()); 
		values[0] = 0; 
		for(int i=0; i<n; i++){ 
			
			st = new StringTokenizer(br.readLine()); 
			int cost = Integer.parseInt(st.nextToken()); 
			int reward = Integer.parseInt(st.nextToken()); 
			
			for(int j=reward; j<c+101; j++){ 
				int prev = values[j-reward]; 
				if(prev!=Integer.MAX_VALUE) values[j] = Math.min(values[j], cost+prev); 
			} 
		} 
		
		
		int result = Integer.MAX_VALUE; 
		
		
		for(int i=c; i<c+101;i++){ 
			result = Math.min(result,values[i]); 
		} 
		
		
		System.out.println(result); br.close(); 
	}


}

좋은 웹페이지 즐겨찾기