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

#PS0000172. Tọa độ xa nhất của robot (Wandering)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tọa độ xa nhất của robot (Wandering)

Wandering

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A robot starts at coordinate 0 on a number line. Given a1,…,ana_1,\ldots,a_n, in stage ii (i=1,…,ni=1,\ldots,n) it makes successive displacements a1,a2,…,aia_1,a_2,\ldots,a_i from its current position. Negative displacements move left. It does not reset between stages. Find the largest coordinate reached, including its initial position.

Input

The first line contains integer nn.

The second line contains the nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print one integer: the largest coordinate reached.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; −108≤ai≤108-10^8\le a_i\le 10^8.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; 0≤ai≤1080\le a_i\le 10^8.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −108≤ai≤108-10^8\le a_i\le 10^8.

Example 1

Input

3
2 -1 -2

Output

5

Explanation

Positions after successive moves are 2; 4, 3; 5, 4, 2. The maximum is 5.

Example 2

Input

5
-2 1 3 -1 -1

Output

2

Explanation

Stage starting positions are 0, -2, -3, -1, 0; the last stage reaches 2.

Example 3

Input

5
-1000 -1000 -1000 -1000 -1000

Output

0

Explanation

All movements are negative, so the initial position is maximal.