본문 바로가기
알고리즘 & 자료구조/백준

백준 21758

by 신재권 2022. 1. 13.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main21758 {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
		int N = Integer.parseInt(br.readLine()); //  꿀통의 개수N
		int[] honey = new int[N+1];
		int[] sum = new int[N+1];
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=1;i<=N; i++) {
			honey[i] = Integer.parseInt(st.nextToken());
			sum[i] = honey[i]+ sum[i-1];
		}

		int ans = 0;
		
		//벌 벌 벌통
		for(int i=2; i<=N-1; i++) {
//	       			 |      첫번째  벌 		   	   |   두번째 벌 
			int tmp = sum[N] - honey[1] - honey[i] + sum[N] - sum[i];		
			ans = Math.max(ans, tmp);
		}
		
		//벌 벌통 벌
		for(int i=2; i<=N-1; i++) {
			//         오른쪽 벌         |  왼쪽벌  
			int tmp = sum[N-1]-sum[i-1]+sum[i]-sum[1];
			ans = Math.max(ans, tmp);
		}
		
		//벌통 벌 벌 
		for(int i=2; i<=N-1; i++) {
			//         첫번째 벌  |           두번째벌 
			int tmp = sum[N-1] - honey[i] + sum[i-1];
			ans = Math.max(ans, tmp);
		}
		System.out.println(ans);
		
		
		
		
	}
	
	
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 2780  (0) 2022.01.15
백준 2579  (0) 2022.01.14
백준 3976  (0) 2022.01.12
백준 5619  (0) 2022.01.11
백준 1455  (0) 2022.01.10