[백준/c++] 11399번: ATM

somyeong·2022년 6월 7일
0

백준

목록 보기
34/45

문제 링크 - https://www.acmicpc.net/problem/11399

🌱 문제


🌱 풀이 및 코드

  • 풀이 : 인출하는데 적게 걸리는 사람이 앞에 와야 결과적으로 걸리는 시간의 합이 최소가 된다.
  • 그러므로, 오름차순으로 정렬한 후, 각 사람의 걸리는 시간을 배열에 저장한 후, 그 값들을 더하면 모든사람의 총합이 된다.
//11399. ATM
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n;
int arr[1001];
int answer;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    vector<int> v(n);
    for(int i=0; i<n; i++){
        cin>>v[i];
    }

    sort(v.begin(), v.end()); //오름차순 정렬

    //arr[i]: i번째 사람이 돈을 인출하는데 총 걸리는 시간(기다리는시간 + 걸리는 시간)
    arr[0]=v[0]; 
    for(int i=1; i<n; i++){
        arr[i]=arr[i-1]+v[i]; 
    }

    //answer: 걸리는 시간들의 합
    for(int i=0; i<n; i++){
        answer+=arr[i];
    }

    cout<<answer<<"\n";
}
profile
공부한 내용 잊어버리지 않게 기록하는 공간!

0개의 댓글