본문 바로가기

Coding Test/백준

[백준/자바] 11399 - ATM

Arrays.sort()를 이용한 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] arr = new int[N];

        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        Arrays.sort(arr);

        int result = 0;
        for(int i=0; i<N; i++) {
            result += arr[i] * (N - i);
        }

        System.out.println(result);

        br.close();
    }
}

삽입 정렬을 이용한 풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] A = new int[N];
        int[] S = new int[N];
        for(int i=0; i<N; i++) {
            A[i] = sc.nextInt();
        }

        for(int i=1; i<N; i++) {
            int point = i;
            int value = A[i];
            for(int j=i-1; j>=0; j--) {
                if(A[j] < A[i]) {
                    point = j+1;
                    break;
                }
                if(j == 0) {
                    point = 0;
                }
            }

            for(int j=i; j>point; j--) {
                A[j] = A[j-1];
            }
            A[point] = value;
        }

        S[0] = A[0];
        for(int i=1; i<N; i++) {
            S[i] = S[i-1] + A[i];
        }
        int sum = 0;
        for(int i : S) {
            sum += i;
        }

        System.out.println(sum);
    }
}