백준 문제/DP

[백준] 5953번 Profits

dubu0721 2025. 2. 17. 15:54

문제: 5953번: Profits

basic-algo-lecture/workbook/0x10.md at master · encrypted-def/basic-algo-lecture

 

basic-algo-lecture/workbook/0x10.md at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <limits>
//#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;
	vector<int> nums(n);
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}

	int answer = nums[0];
	for (int i = 1; i < n; i++) {
		nums[i] = max(nums[i - 1] + nums[i], nums[i]);

		if (nums[i] > answer)
			answer = nums[i];
	}
	cout << answer;

	return 0;
}

 

연속합 문제랑 똑같은 풀이로 해결할 수 있었다. 자신의 값, 자신의 값 + 이전 값 둘 중 더 큰 값으로 nums 값을 갱신해주면 된다.