This page is still under construction.

Parts of this page are still being built. What you see may change.

Cake

Interview

Time limit1sMemory limit128 MB

Summary
Partition the sequence of slice lengths into consecutive blocks, bottom to top, so each block's sum is at least the one above, and maximize the number of blocks.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Prefix sum, Binary search
Solved
No attempts yet

Problem

Friends are baking a birthday cake to celebrate Jaemin's birthday.

To make the cake, bread slices of height 11 and lengths W1,W2,…,WNW_1, W_2, \dots, W_N are baked one after another, in order from 11 to NN. All of these slices must be stacked (none may be thrown away) to build a layered cake.

The cake can consist of several layers. The length of a layer is the sum of the lengths of the slices placed on that layer. So that the cake does not collapse, the length of an upper layer must not be greater than the length of the layer directly below it.

Also, a slice baked later may not be placed on a lower layer than a slice baked earlier. In other words, each layer holds a block of consecutively numbered slices, and the slice numbers increase as you go from the bottom layer to the top.

Stacking the cake as high as possible while satisfying all of these conditions, how many layers can you make?

Input

The first line contains the number of slices NN. (1≤N≤100 0001 \le N \le 100\,000)

Each of the next NN lines contains one slice length WiW_i. (1≤Wi≤10 0001 \le W_i \le 10\,000)

Output

Print the maximum number of layers the cake can have.

Hint

For example, if the slice lengths are 1,2,31, 2, 3 in order, they can be stacked as follows.

+----------+
|    3     |
+---+------+
| 1 |   2  |
+---+------+

The bottom layer consists of slices 11 and 22 (total length 33) and the top layer consists of slice 33 (length 33), giving two layers.

Examples1

  1. Example 1

    Input
    3
    1
    2
    3
    
    Expected output
    2