Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.7 kB
1
Indexable
Never
package practice;

import java.io.FileInputStream;
import java.util.Scanner;

public class banbongbay {
	static int N,max,sum;
	static boolean[] check;
	static int[] bong;
	static int tinhdiem(int x) {
		
		int a = 0;
		int b =0;
		for(int i =x-1; i>=0;i--) {
			if(check[i] == false) {
				a= bong[i];
				break;
				
				
			}
			else {
				a =1;
			}
		}
		for(int i =x+1; i<N;i++) {
			if(check[i] == false) {
				b= bong[i];
				break;
				
				
			}
			else {
				b =1;
			}
		}
		return a*b;
	}	
//	static int tinhdiem2() {
//		int max1 =0;
//		for(int i =0; i<N;i++) {
//			if(check[i] == false) {
//				if(max1 < bong[i]) {
//					max1 = bong[i];
//				}
//			}
//		}
//		return max1*2;
//	}
	static void try1(int index) {
		if(index == N-2) {
			int max1 =0;
			for(int i =0; i<N;i++) {
				if(check[i] == false) {
					if(max1 < bong[i]) {
						max1 = bong[i];
					}
				}
			}
			int sumt = sum+2*max1;
			if(max < sumt) {
				max = sumt;
			}
			return;
		}
		for(int i = 0; i<N;i++) {
			if(check[i] == false) {
				check[i] = true;
				int t = tinhdiem(i);
				sum+= t;
				try1(index+1);
				check[i] = false;
				sum-= t;
			}
		}
		
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("src/input.txt"));
		Scanner scanner = new Scanner(System.in);
		int T = scanner.nextInt();
		for(int t =1;t<= T;t++) {
			System.out.println("Case #"+t);
			N = scanner.nextInt();
			bong = new int[N];
			for(int i =0;i<N;i++) {
				bong[i] = scanner.nextInt();
			}
			max =0;
			sum =0;
			check = new boolean[N];
			try1(0);
			
			System.out.println(max);
			
		}
	}

}
Leave a Comment