Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class TwoFourEight {
  5. static int[] list;
  6. static int max;
  7. static int[][] dp;
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader br = new BufferedReader(new FileReader("248.in"));
  10. PrintWriter pw = new PrintWriter(new FileWriter("248.out"));
  11. StringTokenizer st = new StringTokenizer(br.readLine());
  12.  
  13. int n = Integer.parseInt(st.nextToken());
  14. list = new int[n];
  15. max = Integer.MIN_VALUE;
  16. dp = new int[n+1][n];
  17. for (int i = 0; i < n; i++) {
  18. st = new StringTokenizer(br.readLine());
  19. list[i] = Integer.parseInt(st.nextToken());
  20. max = Math.max(list[i], max);
  21. }
  22.  
  23. pw.println(maxNum(n,n-1));
  24. pw.close();
  25. }
  26.  
  27. public static int maxNum(int n, int i){
  28. if(dp[n][i] > 0)
  29. return dp[n][i];
  30. if(n == 1)
  31. return dp[n][i] = list[0];
  32. if(i == 0)
  33. return dp[n][i] = max;
  34. if(list[i] == list[i-1]) {
  35. list[i-1]++;
  36. for(int x = i; x<n-1; x++)
  37. list[x] = list[x+1];
  38. //System.out.println(Arrays.toString(list));
  39. max = Math.max(max, list[i-1]);
  40. return dp[n][i] = Math.max(max, maxNum(n - 1, n - 2));
  41. }
  42. max = Math.max(max, list[i]);
  43. return dp[n][i] = Math.max(max, maxNum(n, i-1));
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement