[백준/C/Python] 11399 - ATM

orangesnail·2024년 9월 10일

백준

목록 보기
26/169
post-thumbnail

https://www.acmicpc.net/problem/11399


구현 과정

문제가 복잡해 보이지만 뜯어보면 사실 정렬만 하면 되는 간단한 문제다. 필요한 시간의 합의 최솟값은 시간이 오름차순으로 정렬된 경우에서의 합이다. 이를 위해 c에서는 qsort() 를 사용하고, 파이썬에서는 sort() 를 사용해 정렬한 후, 필요한 시간의 누적합의 합을 구해 출력한다.


전체 코드

C

#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main() {
    int n;
    scanf("%d", &n);

    int time[n];
    for (int i = 0; i < n; i++)
        scanf("%d", &time[i]);

    qsort(time, n, sizeof(int), compare);

    int count = 0;
    int totalTime = 0;
    for (int i = 0; i < n; i++) {
        count += time[i];
        totalTime += count;
    }
    printf("%d\n", totalTime);
    return 0;
}

Python

n = int(input())
time = list(map(int, input().split()))

time.sort()

count = 0
totalTime = 0
for t in time:
    count += t
    totalTime += count

print(totalTime)
profile
초보입니다. 피드백 환영합니다 😗

0개의 댓글