#P4409. [ZJOI2006] 皇帝的烦恼

    ID: 3325 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心递推2006各省省选浙江中国剩余定理,CRT

[ZJOI2006] 皇帝的烦恼

Description

After years of warfare, the First Emperor of Qin finally unified China. To defend against foreign invasions, he plans to station nn generals along the borders.

Unfortunately, these nn generals have grown powerful and begun to reveal their ambitions. They refuse to report for duty or accept imperial edicts. The emperor has prepared to secretly execute these insolent frontier commanders. However, to prevent a mutiny, he decides to first award them some medals to buy time.

The generals are delighted to hear they will be awarded medals and each sends a letter of thanks. The ii-th general requests aia_i medals of different colors. However, the generals are proud: if two adjacent generals possess medals of the same color, they will consider it disrespectful and immediately rebel (generals numbered ii and i+1i+1 are adjacent; because their border garrisons can be regarded as forming a circle, generals 11 and nn are also adjacent).

The emperor must satisfy each general’s request, but he is furious at their arrogance. He decides to cast as few colors of medals as possible to meet their demands. What is the minimum number of medal colors he needs to cast?

Input Format

The first line contains an integer nn.

The next nn lines each contain an integer aia_i, the number of different colors of medals requested by the ii-th general.

Output Format

Output a single integer, the minimum number of colors required.

4
2
2
1
1
4

Hint

1n2×1041 \leq n \leq 2 \times 10^4, 1ai1051 \leq a_i \leq 10^5.

Translated by ChatGPT 5