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

#GD0000096. Uống nhiều thuốc nhất – bản khó (Potions (Hard Version))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Uống nhiều thuốc nhất – bản khó (Potions (Hard Version))

Potions (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn potions in order; taking potion ii changes health by aia_i, which may be negative. Starting at zero health, you may take or skip each potion while moving left to right. Health must remain nonnegative after every taken potion. Maximize the number taken.

Input

Line 1 contains nn. Line 2 contains nn integers a1,…,ana_1,\ldots,a_n.

Output

Print the maximum possible number of potions taken.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; −109≤ai≤109-10^9\le a_i\le10^9 for every ii.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; −109≤ai≤109-10^9\le a_i\le10^9 for every ii.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −109≤ai≤109-10^9\le a_i\le10^9 for every ii.

Examples

Input

6
4 -4 1 -3 1 -3

Output

5

Explanation

Choose potions 1,3,4,5,61,3,4,5,6, obtaining health levels 4,5,2,3,04,5,2,3,0. Taking all six would give negative total health, so five is optimal.