Coding - Algo/Java
[백준] 14728번:벼락치기(Java 자바)
jainn
2021. 12. 1. 17:22
728x90
문제
https://www.acmicpc.net/problem/14728
14728번: 벼락치기
ChAOS(Chung-ang Algorithm Organization and Study) 회장이 되어 일이 많아진 준석이는 시험기간에도 일 때문에 공부를 하지 못하다가 시험 전 날이 되어버리고 말았다. 다행히도 친절하신 교수님께서 아래와
www.acmicpc.net
풀이 및 소스코드
아래 블로그를 참고해 풀었다. 설명이 아주 잘 되어있음!!!
dp 넘나 어려운 것 ㅠㅠ....
https://moonsbeen.tistory.com/311
[백준]14728: 벼락치기 - JAVA
[백준]14728: 벼락치기 14728번: 벼락치기 ChAOS(Chung-ang Algorithm Organization and Study) 회장이 되어 일이 많아진 준석이는 시험기간에도 일 때문에 공부를 하지 못하다가 시험 전 날이 되어버리고 말았다..
moonsbeen.tistory.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
node[] unit = new node[n+1];
for(int i=1;i<=n;i++) {
st = new StringTokenizer(br.readLine());
int k = Integer.parseInt(st.nextToken());
int s = Integer.parseInt(st.nextToken());
unit[i] = new node(k, s);
}
int[][] dp = new int[n+1][t+1];
for(int i=1;i<=n;i++) {
for(int j=0;j<=t;j++) {
if(unit[i].k<=j) {
dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-unit[i].k]+unit[i].s);
}else {
dp[i][j] = dp[i-1][j];
}
}
}
System.out.println(dp[n][t]);
}
}
class node{
int k, s;
public node(int k, int s) {
super();
this.k = k;
this.s = s;
}
}
반응형