CS/알고리즘_문제풀이(자바)

퇴사

Jedy_Kim 2021. 10. 18. 18:46
728x90

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

 

14501번: 퇴사

첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.

www.acmicpc.net

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.*;
import java.io.*;
 
class Main{  
  
  static int N;  
  static int[][] arr;
  static int[] dp;
   
  public static void main(String[] args) throws Exception {
    // Please Enter Your Code Here
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    N   = Integer.parseInt(br.readLine());
    arr = new int[N][2];
    dp  = new int[N + 1];
    
    for(int i=0; i<N; ++i) {
      arr[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    for(int i=N-1; i>-1--i) {
      if( (i + arr[i][0]) > N ) dp[i] = dp[i+1];
      else dp[i] = Math.max( (arr[i][1+ dp[i + arr[i][0]]), dp[i+1]);
    }
    
    bw.write(String.valueOf(dp[0]));
    br.close();
    bw.flush();
    bw.close();
  }
}
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

가장 긴 증가하는 부분 수열  (0) 2021.10.18
탈출  (0) 2021.10.18
  (0) 2021.10.15
벽 부수고 이동하기  (0) 2021.10.15
부분수열의 합  (0) 2021.10.14