Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class TwoFourEight {
- static int[] list;
- static int max;
- static int[][] dp;
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("248.in"));
- PrintWriter pw = new PrintWriter(new FileWriter("248.out"));
- StringTokenizer st = new StringTokenizer(br.readLine());
- int n = Integer.parseInt(st.nextToken());
- list = new int[n];
- max = Integer.MIN_VALUE;
- dp = new int[n+1][n];
- for (int i = 0; i < n; i++) {
- st = new StringTokenizer(br.readLine());
- list[i] = Integer.parseInt(st.nextToken());
- max = Math.max(list[i], max);
- }
- pw.println(maxNum(n,n-1));
- pw.close();
- }
- public static int maxNum(int n, int i){
- if(dp[n][i] > 0)
- return dp[n][i];
- if(n == 1)
- return dp[n][i] = list[0];
- if(i == 0)
- return dp[n][i] = max;
- if(list[i] == list[i-1]) {
- list[i-1]++;
- for(int x = i; x<n-1; x++)
- list[x] = list[x+1];
- //System.out.println(Arrays.toString(list));
- max = Math.max(max, list[i-1]);
- return dp[n][i] = Math.max(max, maxNum(n - 1, n - 2));
- }
- max = Math.max(max, list[i]);
- return dp[n][i] = Math.max(max, maxNum(n, i-1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement