View Code of Problem 3909

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		int n =in.nextInt();
		StringBuffer num=new StringBuffer();
		for(int i =0;i<n;i++) {
			num.append(in.nextInt());
		}
		System.out.println(kill(num,0));
	}
	public static int kill(StringBuffer num,int point) {
		if(num.length()==2) {
			return point;
		}else {
			int max = Integer.MIN_VALUE;
			for(int i =0;i<num.length();i++) {
				char del = num.charAt(i);
				int p;
				if(i!=0 && i!=num.length()-1) {
					int plus = min(num.charAt(i-1)-'0',num.charAt(i+1)-'0');
					num.deleteCharAt(i);
					p = kill(num,point+plus);
				}else {
					num.deleteCharAt(i);
					p = kill(num,point);
				}
				num.insert(i, del);
				if(max<p) {
					max=p;
				}
			}
			return max;
		}
	}
	public static int min(int a ,int b) {
		if(a>b) {
			return b;
		}else {
			return a;
		}
	}
}

Double click to view unformatted code.


Back to problem 3909