https://www.acmicpc.net/problem/21758
package org.example;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int n;
private static int[] honey;
private static int total, ans;
private static int[] toLeftSum, toRightSum;
public static void case1(){
for(int i = 1; i < n-1; i++){
int bee1 = total - ( honey[0] + honey[i] );
int bee2 = total - toRightSum[i];
ans = Math.max(ans, bee1 + bee2);
}
}
public static void case2(){
for(int i = 1; i < n-1; i++){
int bee1 = total - ( honey[n-1] + honey[i] );
int bee2 = total - toLeftSum[i];
ans = Math.max(ans, bee1 + bee2);
}
}
public static void case3(){
for(int i = 1; i < n -1; i++){
int bee1 = toRightSum[i] - honey[0];
int bee2 = toLeftSum[i] - honey[n-1];
ans = Math.max(ans, bee1 + bee2);
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
honey = new int[n];
//왼쪽부터 오른쪽으로 가는 누적합 배열
toRightSum = new int[n];
//오른쪽부터 왼쪽으로 가는 누적합 배열
toLeftSum = new int[n];
int tempSum = 0;
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i =0 ; i< n; i++){
honey[i] = Integer.parseInt(st.nextToken());
tempSum += honey[i];
toRightSum[i] = tempSum;
}
total = toRightSum[n-1];
tempSum = 0;
for(int i = n -1; i >= 0; i--){
tempSum += honey[i];
toLeftSum[i] = tempSum;
}
ans = 0; //최대의 꿀의 채집량
case1();
case2();
case3();
System.out.println(ans);
}
}
https://velog.io/@silver_star/%EB%B0%B1%EC%A4%80-21758-%EA%BF%80-%EB%94%B0%EA%B8%B0-Greedy