Algorithm/Problems
99클럽 코테 스터디 5일차 TIL + 누적합
공부좀하시졍
2025. 4. 5. 00:42
https://www.acmicpc.net/problem/2559
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
temps = list(map(int, input().split()))
partSum = sum(temps[:k])
result = [partSum]
for i in range(n-k): # n개의 수를 k씩 연속합을 구하면 나오는 갯수
result.append(result[i]-temps[i]+temps[i+k])
print(max(result))
- 합을 기록하는 리스트의 이전값(result[i]) 에서 제일 왼쪽 값 (temps[i])을 제외하고 그 다음값( temps[i+k]) 을 더하면 다음 누적합을 구할 수 있다.
- n-k는 n개의 수를 k개씩 연속합을 구하면 나오는 가짓수 이다.