백준 2559

HR·2022년 4월 3일
0

알고리즘 문제풀이

목록 보기
6/50

백준 2559 : 수열

  1. 구간 합 이용 (a[i] = a[i-1] + temp)
  2. max 함수를 이용해 최대값을 갱신

정답 코드

#include <bits/stdc++.h>

using namespace std;

int n, k, d[100001];
int ans=-10000000;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin>>n>>k;
	
	int temp;
	for(int i=1; i<=n; i++) {
		cin>>temp;
		d[i]=d[i-1]+temp;
	}
	
	for(int i=k; i<=n; i++) {
		ans = max(ans, d[i]-d[i-k]);
	}
	
	cout<<ans<<"\n";
		
	
	return 0;
}

0개의 댓글