백준 2828번

DARTZ·2023년 3월 30일
0

알고리즘

목록 보기
104/135
import sys

sys.stdin = open("input.txt", "r")

N, M = map(int, sys.stdin.readline().split())
J = int(sys.stdin.readline())
apples = []
for _ in range(J):
    apples.append(int(sys.stdin.readline()))

# n, m = map(int, input().split())
# k = input()
# data = list(map(int, input().split()))
# data = sys.stdin.readline().rstrip()

# data = sys.stdin.readline()


def solution(N, M, J, apples):

    start = 1
    count = 0
    end = M

    for apple in apples:
        while start > apple or apple > end:
            if apple > end and end <= N:
                start += 1
                end += 1

            else:
                start -= 1
                end -= 1

            count += 1

    return count


print(solution(N, M, J, apples))
profile
사람들이 비용을 지불하고 사용할 만큼 가치를 주는 서비스를 만들고 싶습니다.

0개의 댓글