๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 4097_์ˆ˜์ต

๋ฟŒ์•ผ._. 2026. 1. 30. 11:28
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/4097)

< ์ˆ˜์ต >

 

๋ฌธ์ œ ํ’€์ด 

 

dp๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์€ ์ˆ˜์ต์„ ์˜ฌ๋ฆฐ ๊ตฌ๊ฐ„์˜ ์ˆ˜์ต์„ ๊ตฌํ•œ๋‹ค. 

 

my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class _4097_ { // ์ˆ˜์ต

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int n = 0;

		while ((n = Integer.parseInt(bf.readLine())) != 0) {

			int arr[] = new int[n];
			int dp[] = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(bf.readLine());
				dp[i] = arr[i];
			}

			int result = Integer.MIN_VALUE;

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

			bw.write(result + "\n");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
n : ์ผ
arr : ๋งค์ผ๋งค์ผ์˜ ์ˆ˜์ต
dp : ๋งŽ์€ ์ˆ˜์ต์„ ์˜ฌ๋ฆฐ ๊ตฌ๊ฐ„์˜ ์ˆ˜์ต
result : ๊ฐ€์žฅ ๋งŽ์€ ์ˆ˜์ต์„ ์˜ฌ๋ฆฐ ๊ตฌ๊ฐ„์˜ ์ˆ˜์ต 

 

0์ด ์ž…๋ ฅ๋˜๊ธฐ ์ „๊นŒ์ง€ n์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. n๋งŒํผ ๋งค์ผ๋งค์ผ์˜ ์ˆ˜์ต์„ ์ž…๋ ฅ๋ฐ›์•„ arr๊ณผ dp์— ์ €์žฅํ•œ๋‹ค. ๋งค์ผ๋งค์ผ์„ ์‚ดํŽด๋ณด๋ฉฐ ํ˜„์žฌ ๊ฐ’๊ณผ, ์ „๋‚ ์˜ ๊ฐ’+ํ˜„์žฌ ๊ฐ’ ๋‘˜ ์ค‘์— ํฐ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. ์ตœ์ข… ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 5953_Profits  (0) 2026.02.10
[Baekjoon] 1699_์ œ๊ณฑ์ˆ˜์˜ ํ•ฉ  (0) 2026.02.09
[Baekjoon] 18353_๋ณ‘์‚ฌ ๋ฐฐ์น˜ํ•˜๊ธฐ  (0) 2026.01.29
[Baekjoon] 14501_ํ‡ด์‚ฌ  (0) 2026.01.27
[Baekjoon] 6221_The Bale Tower  (1) 2026.01.26