[백준] 1249 : 온라인 판매 - Python

Chooooo·2022년 10월 28일
0

알고리즘/백준

목록 보기
30/182

그리디 알고리즘

문제해결
팔 수 있는 달걀의 개수를 미리 정했어야 했다.
오름차순으로 정렬했기에 다음 인덱스일 때는 그 윗부분을 신경쓸 필요가 없다!

생각
이런 형태의 코드 잘 보고 체화해두기.

소스코드

import sys


N, M = map(int, input().split())

data = []
for i in range(M):
    data.append(int(input()))

data.sort()
res = 0 #최대 수익
egg = 0  #달걀을 책정한 가격
for i in range(M):
    cnt = min(M-i, N)  #팔 수 있는 달걀 개수

    if res < data[i] * cnt:
        res = data[i] * cnt
        egg = data[i]


print(egg, res)
profile
back-end, 지속 성장 가능한 개발자를 향하여

0개의 댓글