DMOPC '20 Contest 3 P4 - Bob and Typography

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Bob is taking a course on typography, the art of arranging words! In this course, he is given a phrase with N words consisting only of English letters. He must split the phrase over one or more lines, and he cannot split the phrase in the middle of a word.

For example, he can split the quick brown fox jumps over the lazy dog over four lines like this:

Copy
the quick brown
fox jumps
over
the lazy dog

As any good typographer knows, a split is pretty if the number of letters in each line (not including spaces) form:

  • a non-increasing sequence,
  • a non-decreasing sequence,
  • or a non-increasing then non-decreasing sequence.

More rigorously, let K be the total number of lines and ti be the number of letters in line i. Then a split is pretty if there exists k{0,,K} such that t1tk and tk+1tK.

For example, the following splits are pretty:

Copy
the
quick brown
fox jumps over the lazy dog
Copy
the quick brown
fox jumps
over
the lazy dog
Copy
the quick brown fox jumps over the lazy dog

And the following split is not:

Copy
the quick
brown fox jumps
over the lazy
dog

Over all pretty splits, please help Bob find the one with the maximum number of lines!

Constraints

Each word contains at least 1 and no more than 3000 letters.

Subtask 1 [2/15]

1N20

Subtask 2 [3/15]

1N100

Subtask 3 [3/15]

1N700

Subtask 4 [6/15]

1N5000

Subtask 5 [1/15]

1N30000

Input Specification

The first line contains N, the number of words in the phrase.
The second line contains N space-separated integers, the number of letters in each word.

Output Specification

The number of lines in the longest possible pretty split.

Sample Input

Copy
9
3 5 5 3 5 4 3 4 3

Sample Output

Copy
6

Explanation for Sample Output

This input corresponds to the phrase given in the problem description. One longest pretty split for that phrase is:

Copy
the quick
brown
fox
jumps
over the
lazy dog

with 6 lines.


Comments

There are no comments at the moment.