less than 1 minute read

문제접근

처음부터 지금까지 다줍는 경우를 저장하는 배열
지금까지 주운것 중에서 가장적은 경우를 저장하는 배열
지금까지 주운 값어치 - 지금까지 최악의 값어치(m)
언제나 0보다 커야하기에 max로 비교

코드

#include <bits/stdc++.h> 
#define ll long long

using namespace std;
int dp[100001];
int worst[100001];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, m; cin >> n >> m;
	int tmp;
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> tmp;
		dp[i] = tmp + dp[i - 1];
		worst[i] = min(dp[i], worst[i - 1]);
		if(i >= m)
			ans = max(ans, max(dp[i], dp[i] - worst[i - m]));
	}
	cout << ans;
	return 0;
}

Leave a comment