본문 바로가기
Programming/알고리즘

[java, 알고리즘, 그리디] :: 백준 - ATM

by 구튼탁 2021. 2. 17.
728x90

이전글 :: 2021/02/17 - [Programming/알고리즘] - [java, 알고리즘, 그리디] :: 1이 될 때까지

 


www.acmicpc.net/problem/11399

 

11399번: ATM

첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)

www.acmicpc.net

package com.algorithm.greedyEx04;

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

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

        String [] arr = br.readLine().split(" ");

        int [] numArr = new int[n];

        for(int i = 0; i < n; i++){
            numArr[i] = Integer.parseInt(arr[i]);
        }
        Arrays.sort(numArr);

        int result = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < i + 1; j++){
                result += numArr[j];
            }
        }
        System.out.println(result);
    }
}

접근방법

인출 소요 시간이 적은 순서로 정렬한다.

 

 

728x90

댓글