"""
출처:프로그래머스,
https://school.programmers.co.kr/learn/courses/30/lessons/42583
"""
# 풀이과정
def solution(bridge_length, weight, truck_weights):
from collections import deque
b_l = bridge_length # 최대 수용 가능 차량 수
w = weight # 무게
t_w = deque(truck_weights)
now = 0 # 시간
count_w = 0 # 다리 위 차량 무게
count_c = 0 # 차량 수
check = deque([])
while t_w:
k = t_w.popleft()
if len(check) == 0:
check.append([now, k])
count_w += k
count_c += 1
else:
if now - check[0][0] == b_l: # 차가 다리를 벗어나는 시점
i, j = check.popleft()
count_w -= j
count_c -= 1
if count_w + k <= w and count_c + 1 <= b_l:
check.append([now, k])
count_w += k
count_c += 1
else:
t_w.appendleft(k)
now += 1
# print(check)
continue
if count_w + k <= w and count_c + 1 <= b_l:
check.append([now, k])
count_w += k
count_c += 1
else:
t_w.appendleft(k)
now += 1
# print(check)
return check[-1][0] + b_l + 1