Little Jay

[C++] 백준 1912번 - 연속합 본문

알고리즘/BOJ

[C++] 백준 1912번 - 연속합

Jay, Lee 2021. 8. 21. 14:59

DP를 사용하는 문제

DP의 합과 입력받은 arr의 숫자를 잘 비교하면 되는 것 같다.

 

#include <iostream>
#include <vector>
using namespace std;

int arr[100001]{ 0, };
int dp[100001]{ 0, };

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}

	dp[1] = arr[1];
	int result = dp[1];

	for (int i = 2; i <= n; i++) {
		dp[i] = max(dp[i - 1] + arr[i], arr[i]);
		result = max(result, dp[i]);
	}

	cout << result << "\n";

	return 0;
}

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 11508번 - 2 + 1 세일  (0) 2021.08.21
[C++] 백준 2702번 - 초6 수학  (0) 2021.08.21
[C++] 백준 1850번 - 최대공약수  (0) 2021.08.21
[C++] 백준 5430번 틀  (0) 2021.08.20
[C++] 백준 9093번 - 단어 뒤집기  (0) 2021.08.19
Comments