ํ์ด
๋ฐ๋ก ๋๋ฌธ์ ์กฐ๊ธ ๊ณ ์ํ๋ ๋ฌธ์ ์ธ๋ฐ์, ์ผ์ด์ค๋ณ๋ก ๋ฌถ๋ ๊ฒ์ด ํต์ฌ์ธ ๋ฌธ์ ์์ต๋๋ค. ์ด๋ ๊ธฐ์ค์ 1๋ก ์ผ์์ผ ํ๋๋ฐ์, 1์ ๋ค๋ฅธ ์์ ๊ณฑํ๋ฉด ์๊ธฐ ์์ ์ด ๋์ค๊ธฐ ๋๋ฌธ์ ๋ฌด์กฐ๊ฑด ๋ํด์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ฐ๋ผ์ 1 ๋ฏธ๋ง์ผ ๊ฒฝ์ฐ, 1์ผ ๊ฒฝ์ฐ, 1์ด๊ณผ์ผ ๊ฒฝ์ฐ๋ก ์ด๋ฅผ ๊ฐ๊ฐ ๋ถ๋ฅํด์ค ๋ค ๋ ๊ฐ์ฉ ๋ฌถ์ด์ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค. ์ด๋ ๊ฐ์๊ฐ ์ง์/ํ์์ธ ๊ฒฝ์ฐ๋ ๊ณ ๋ คํด์ค์ผ ํ๋๋ฐ, ํ์์ธ ๊ฒฝ์ฐ ๋๊ฐ์ฉ ๋ฌถ๊ณ ๋์ ๋ง์ง๋ง ์๋ฅผ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
public class Main {
public static void main(String[] args) throws Exception {
int n = input.integer();
List<Integer> plus = new ArrayList<>();
List<Integer> others = new ArrayList<>();
List<Integer> minus = new ArrayList<>();
for (int index = 0; index < n; index++) {
int number = input.integer();
if (number > 1) {
plus.add(number);
;
} else if (number == 1) {
others.add(number);
} else {
minus.add(number);
}
}
Collections.sort(plus, Collections.reverseOrder());
Collections.sort(others);
Collections.sort(minus);
int sum = 0;
if (plus.size() != 0) {
if (plus.size() % 2 != 0) {
for (int index = 0; index < plus.size() - 1; index += 2) {
sum += plus.get(index) * plus.get(index + 1);
}
sum += plus.get(plus.size() - 1);
} else {
for (int index = 0; index < plus.size(); index += 2) {
sum += plus.get(index) * plus.get(index + 1);
}
}
}
if (others.size() != 0) {
for (int index = 0; index < others.size(); index++) {
sum += others.get(index);
}
}
if (minus.size() != 0) {
if (minus.size() % 2 != 0) {
for (int index = 0; index < minus.size() - 1; index += 2) {
sum += minus.get(index) * minus.get(index + 1);
}
sum += minus.get(minus.size() - 1);
} else {
for (int index = 0; index < minus.size(); index += 2) {
sum += minus.get(index) * minus.get(index + 1);
}
}
}
System.out.println(sum);
}
static Input input = new Input();
static class Input {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public int integer() throws Exception {
if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public String next() throws Exception {
if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public char[] nToCharArray() throws Exception {
if (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
return st.nextToken().toCharArray();
}
}
}
'๐ PS > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
boj-1916 (0) | 2022.08.24 |
---|---|
boj-2252 (0) | 2022.08.22 |
boj-1850 (0) | 2022.07.23 |
boj-1922 ๋คํธ์ํฌ ์ฐ๊ฒฐ (0) | 2022.07.19 |
boj-1300 K๋ฒ์งธ ์ (0) | 2022.07.14 |
๋๊ธ