https://www.acmicpc.net/problem/21758
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';
let input = fs.readFileSync(filePath).toString().trim().split('\n');
const n = +input.shift();
const arr = input.shift().split(' ').map(Number);
const sum = new Array(n + 1).fill(0);
let ans = 0;
arr.unshift(0);
for (let i = 1; i <= n; i++) {
sum[i] = arr[i] + sum[i - 1];
}
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[n] - arr[n] - arr[i] + sum[i - 1]);
}
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[n] - arr[1] - arr[i] + sum[n] - sum[i]);
}
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[i] - arr[1] + sum[n] - sum[i - 1] - arr[n]);
}
console.log(ans);
✔ 알고리즘 : 그리디
✔ 가능한 경우의 수는 총 3가지이다
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[n] - arr[n] - arr[i] + sum[i - 1]);
}
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[i] - arr[1] + sum[n] - sum[i - 1] - arr[n]);
}
for (let i = 2; i < n; i++) {
ans = Math.max(ans, sum[n] - arr[1] - arr[i] + sum[n] - sum[i]);
}
✔ 난이도 : 백준 기준 실버 1