白駿2846上り坂



白駿2846上り坂

実施前の考え方


上り坂の第1項と最後の項を除いて、元素の差の和を求めることに等しい.

残念な点


Edge case~範囲をよく考えてみましょう

コード#コード#

package backjoon_4월;

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class backjoon_2846_오르막길 {
	static int N;
	static int height[];
	static int max;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		height = new int[N+1];
		StringTokenizer st = new StringTokenizer(br.readLine()," ");
		for (int i = 0; i < N; i++) {
			height[i]= Integer.parseInt(st.nextToken());
			
		}
		int hill =0;
		int max = 0;
		for (int i = 0; i <= N-1; i++) {
			if(height[i]<height[i+1]) {
				hill+=height[i+1]-height[i];
			}
			if(height[i]>=height[i+1]) {
				max = Math.max(max, hill);
				hill=0;
			}
		}
		
		System.out.println(max);
	}

}