Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static int[][] inputAry;
  6. public static int answer = Integer.MIN_VALUE;
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. int n = sc.nextInt();
  11.  
  12. inputAry = new int[n+1][2];
  13.  
  14. for(int i = 0; i < n; i++) {
  15. inputAry[i][0] = sc.nextInt();
  16. inputAry[i][1] = sc.nextInt();
  17. }
  18.  
  19. dfs(n,0,0);
  20. System.out.println(answer);
  21. }
  22. public static void dfs(int n, int depth, int sum) {
  23. if(depth == n) {
  24. answer = Math.max(answer, sum);
  25. return;
  26. }
  27. if(depth + inputAry[depth][0] <= n) {
  28. dfs(n, depth + inputAry[depth][0], sum + inputAry[depth][1]);
  29. }
  30. dfs(n, depth+1, sum);
  31. }
  32. }
Add Comment
Please, Sign In to add comment