#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
typedef pair<int, int> p;
 
int n, answer;
int highest, lowest;
int qcnt[1001];
vector<int> qual;
 
void init() {
    int input;
    cin >> n;
 
    for (int i = 0; i < n; i++) {
        cin >> input;
        if (qcnt[input] == 0) qual.push_back(input);
        qcnt[input]++;
    }
 
    sort(qual.begin(), qual.end());
 
    lowest = 0;
    highest = qual.size() - 1;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(NULL);
 
    init();
 
    while (1) {
        if (lowest > highest) break;
        int low_cnt = qcnt[qual[lowest]];
        int high_cnt = qcnt[qual[highest]];
        if (lowest == highest) {
            answer += qual[highest] * high_cnt;
            qcnt[highest] = 0;
            lowest++;
            continue;
        }
        if (low_cnt < high_cnt) {
            answer += qual[highest] * low_cnt * 2;
            qcnt[qual[highest]] -= low_cnt;
            qcnt[qual[lowest]] = 0;
            lowest++;
        }
        else {
            answer += qual[highest] * high_cnt * 2;
            qcnt[qual[lowest]] -= high_cnt;
            qcnt[qual[highest]] = 0;
            highest--;
        }
    }
 
    printf("%d\n", answer);
 
    return 0;
}

'Algorithm' 카테고리의 다른 글

Boj_ 회의실 배정 4 java  (0) 2024.11.11
[boj] 이차원 배열과 연산17140 JAVA  (0) 2024.10.27
[boj] 연구소 3_17142 JAVA  (0) 2024.10.21
[boj] Cryptographer’s Conundrum_11269 C++  (0) 2024.10.14
[boj] 양치기꿍_3187 JAVA  (1) 2024.10.07

+ Recent posts