Under construction

Still being built. Things may change

Range Editing

Time limit4sMemory limit2048 MB

Summary
Given a row of n cells with target values (0 means empty), find the minimum number of range assign and range clear operations to reach them.
Level

Hard8 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

A spreadsheet has a row of cells. Initially all cells are empty. You can perform two types of operations:

  1. Select a consecutive range of cells and change their values to a positive integer of your choice. All cells in the range get the same value after this operation.
  2. Select a consecutive range of cells and delete their values. All cells in the range become empty after this operation.

Given the final cell values you want, calculate the minimum number of editing operations required to obtain them.

Input

The first line contains a single integer nn (1≤n≤8001 \leq n \leq 800), the number of cells. The cells are numbered from 11 to nn.

Each of the next nn lines contains a single integer between 00 and 10910^9 inclusive. The integer on the ii-th line is 00 if cell ii should be empty after all operations. Otherwise, it is the final value of cell ii.

Output

Print a single integer, the minimum number of editing operations required.

Examples1

  1. Example 1

    Input
    10
    0
    1
    2
    3
    4
    2
    1
    0
    0
    1
    
    Expected output
    5