Published:
Updated:


Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    static int solution(List<Integer> list) {
        Collections.sort(list); // 오름차순 정렬
        // 3 1 4 3 2 -> 1 2 3 3 4
        // 이제 더해주기만 하면 됨
        int tmp = 0, answer = 0;
        for (int x : list) {
            tmp += x; // 배열 값을 계속 저장해주고
            answer += tmp; // 그 저장한 값을 또 계속 저장한다
        }
        return answer;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        List<Integer> list = new ArrayList<>(n);
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        for (int i = 0; i < n; i++) {
            list.add(Integer.parseInt(st.nextToken()));
        }
        System.out.print(solution(list));
        br.close();
    }
}

Leave a comment