BOJ_S3_14501
π [S3_14501] ν΄μ¬
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
static StringTokenizer st;
static int N;
static int[][] arr;
static int max = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(br.readLine());
arr = new int[N][2];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine(), " ");
// [0] = κΈ°κ° , [1] = κΈμ‘
arr[i][0] = Integer.parseInt(st.nextToken());
arr[i][1] = Integer.parseInt(st.nextToken());
}
dfs(0,0);
sb.append(max);
System.out.println(max);
}
static void dfs(int day,int money) {
// κΈ°μ 쑰건 : κΈ°κ°μ μ΄κ³Όνλ κ²½μ°
if(day >= N) {
max = Math.max(max, money);
return;
}
for(int i=day; i<N; i++) {
if(i+arr[i][0] <= N) {
dfs(i+arr[i][0], money+arr[i][1]);
}
else{
dfs(i+arr[i][0], money);
}
}
}
}
π€ λμ μκ°
dfsλ₯Ό μ΄μ©ν΄μ νμ΄ μ£Όμλ€.
μ²μμ dfsμμμ iλ₯Ό μΌμλ₯Ό λν΄μ€μΌ νλλ° 1μ λν΄μ£Όμ΄μ λ¬Έμ κ° μμλ€.
κ·Έλ¦¬κ³ μΌμλ₯Ό λνμλ Nμ λλ κ²½μ°μ μλλ κ²½μ°λ₯Ό λλμ΄ μ¬κ·ν¨μλ₯Ό ꡬννμλ€.