PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000119. Độ cao lớn nhất (Find the Highest Altitude)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Độ cao lớn nhất (Find the Highest Altitude)

Find the Highest Altitude

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A cyclist travels through n+1n+1 points numbered 0 through nn. The starting altitude at point 0 is 0. The array a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} describes altitude changes: from point ii to point i+1i+1, the altitude increases by aia_i (a negative change means a descent). Find the greatest altitude among all n+1n+1 points, including the starting point.

Input

The first line contains integer nn.

The second line contains nn integers a0,…,an−1a_0,\ldots,a_{n-1}.

Output

Print one integer: the greatest altitude.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; −100≤ai≤100-100\le a_i\le 100; 0≤i<n0\le i<n.
  • Subtask 2 (30 points): 1≤n≤1001\le n\le 100; −100≤ai≤0-100\le a_i\le 0; 0≤i<n0\le i<n.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le 100; −100≤ai≤100-100\le a_i\le 100; 0≤i<n0\le i<n.

Example 1

Input

5
-5 1 5 0 -7

Output

1

Explanation

The six altitudes are 0,−5,−4,1,1,−60,-5,-4,1,1,-6. The maximum is 1 at points 3 and 4.

Example 2

Input

7
-4 -3 -2 -1 4 3 2

Output

0

Explanation

The altitudes are 0,−4,−7,−9,−10,−6,−3,−10,-4,-7,-9,-10,-6,-3,-1. Every point after the start lies below 0, so the answer is 0.