
알고리즘 분류 : 투포인터, 누적합
난이도 : 실버3
출처 : 백준 - 꿀 아르바이트


누적합으로 배열에 값을 넣는다.
누적합을 이용해 M개의 합의 최대값을 구한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
long[] arr = new long[N+1];
long max = 0;
st = new StringTokenizer(br.readLine()," ");
for(int i=1;i<=N;i++) {
arr[i] = arr[i-1]+Integer.parseInt(st.nextToken());
}
for(int i=0;i<N-M+1;i++) {
max = Math.max(max, arr[i+M]-arr[i]);
}
System.out.println(max);
}
}

어려운 문제는 아니었으나 배열을 int가 아닌 long으로 해야 하는점, 0일 근무도 가능하다는 점을 잘 고려해야 한다.