๐Algorithm/๐ฅBaekjoon
[Baekjoon] 17550_Inquiry I
๋ฟ์ผ._.
2026. 3. 4. 10:44
๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/17550)
< Inquiry I >
๋ฌธ์ ํ์ด
์ ์์ ํฉ๊ณผ ์ ์ ์ ๊ณฑ์ ํฉ์ ๋ฏธ๋ฆฌ ๊ตฌํด ์ต๋๊ฐ์ ๊ตฌํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _17550_ { // Inquiry I
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
int arr[] = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
arr[i] = Integer.parseInt(bf.readLine());
}
long square[] = new long[n + 1];
long sum[] = new long[n + 1];
for (int i = 1; i < n + 1; i++) {
square[i] = (arr[i] * arr[i]) + square[i - 1];
sum[i] = arr[i] + sum[i - 1];
}
long result = 0;
for (int i = 1; i < n; i++) {
result = Math.max(result, square[i] * (sum[n] - sum[i]));
}
System.out.println(result);
}
}
๋ณ์)
n : ์ ์ ๊ฐ์
arr : ์ ์
square : ์ ๊ณฑ์ ํฉ
sum : ํฉ
result : ์ต๋๊ฐ
์ ์ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์ ์ ๊ฐ์๋งํผ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ์ arr์ ์ ์ฅํ๋ค. ๋ฏธ๋ฆฌ ์ ๊ณฑ์ ํฉ๊ณผ ํฉ์ ๊ตฌํด ๊ฐ๊ฐ square์ sum ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ์์์๋ถํฐ k๊ฐ์ ์ ๊ณฑ์ ํฉ๊ณผ k+1๋ถํฐ n๊น์ง์ ํฉ์ ๊ณฑํ ๊ฐ์ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
