You are given an array a1,a2,…,ana1,a2,…,an. You can perform the following operation any number of times:

  • Choose a pair of two neighboring equal elements ai=ai+1ai=ai+1 (if there is at least one such pair).
  • Replace them by one element with value ai+1ai+1.

After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array aa you can get?

Input

The first line contains the single integer nn (1≤n≤5001≤n≤500) — the initial length of the array aa.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — the initial array aa.

Output

Print the only integer — the minimum possible length you can get after performing the operation described above any number of times.

Examples

Input

5
4 3 2 2 3

Output

2

Input

7
3 3 4 4 4 3 3

Output

2

Input

3
1 3 5

Output

3

Input

1
1000

Output

1

Note

In the first test, this is one of the optimal sequences of operations: 44 33 22 22 33 →→ 44 33 33 33 →→ 44 44 33 →→ 55 33.

In the second test, this is one of the optimal sequences of operations: 33 33 44 44 44 33 33 →→ 44 44 44 44 33 33 →→ 44 44 44 44 44 →→ 55 44 44 44 →→ 55 55 44 →→ 66 44.

In the third and fourth tests, you can't perform the operation at all.

区间dp

dp数组存l到r里面的个数

当相邻dp数组个数唯一且相等时更新

#include<bits/stdc++.h>
const int maxn = 1e3 + 5;
int num[maxn][maxn];
int dp[maxn][maxn];
using namespace std;int main(){int n;cin >> n;for(int i = 1; i <= n; i++){cin >> num[i][i];dp[i][i] = 1;}for(int i = 1; i <= n; i++){for(int j = i; j <= n; j++){dp[i][j] = j - i + 1;}}for(int len = 2; len <= n; len++){for(int l = 1; l + len - 1 <= n; l++){int r = l + len - 1;for(int k = l; k < r; k++){dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);if(dp[l][k] == 1 && dp[k + 1][r] == 1 && num[l][k] == num[k + 1][r]){dp[l][r] = 1;num[l][r] = num[l][k] + 1;                  }}}}cout << dp[1][n] << endl;
} 

CodeForces - 1312E Array Shrinking 区间dp相关推荐

  1. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  2. CodeForces - 1312E Array Shrinking(区间dp)

    题目链接:点击查看 题目大意:给出 n 个数,现在可执行的操作是: 找到相邻且数值相等的两个数,即 abs( i - j ) == 1 && a[ i ] == a[ j ] 使得两个 ...

  3. codeforces 1312E. Array Shrinking

    https://codeforces.com/problemset/problem/1312/E 傻逼DP又写了快40分钟,迟早药丸 这题之前总是按传统的区间dp想维护i,j的最多合并次数,怎么维护左 ...

  4. codeforces E. Array Shrinking

    题目 题意: 给你一组序列,序列中相邻的两个一样的值可以合并成一个值+1的数,问最后剩下的最少的元素有多少个. 思路: 本来我的思路是用栈或者队列的方式去合并,然后输出元素的个数,但是发现假如1 1 ...

  5. Codeforces 623B Array GCD 数论,dp

    文章目录 题意 题解 题意 一个序列每个数都大于111,要使整个数列的最大公约数大于111,可以最多删除一个子串,每一个数花费aaa元,也可以给一个数增添111或者减少111,每个数最多操作一次.求达 ...

  6. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  7. Codeforces Round #655 (Div. 2) E. Omkar and Last Floor 区间dp + 巧妙的状态设计

    传送门 题意: 思路: 按照贪心的思路来考虑的话,显然是每一列111的个数越多越好,所以我们能放到一列就放到一列.设f[l][r]f[l][r]f[l][r]为在[l,r][l,r][l,r]内,区间 ...

  8. 【CodeForces - 245H 】Queries for Number of Palindromes (带容斥的区间dp)

    题干: You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. The ...

  9. Codeforces Gym100543L Outer space invaders 区间dp 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543L.html 题目传送门 - CF-Gym100543L 题意 $T$ 组数据. 有 $n ...

  10. Codeforces Gym100543L:Outer space invaders(区间DP)

    题目链接 题意 有n个人,每个人有一个出现时间a和一个开枪时间b和一个距离d,在任意一个时刻,你可以选择炸人,你要炸一个人的花费是和他的距离d,并且所有的已经出现并且还没开枪的和你距离<=d的人 ...

最新文章

  1. C++Primer学习——函数
  2. 安装完Pycharm,启动时碰到“failed to load jvm dll“的解决方案
  3. IOS开发之手写约束
  4. 雅客EXCEL(4)-录入数据、超链接阻断、下拉菜单、横列转数列、alt+向下方向键、定位公式
  5. Sharding-JDBC(三)3.1.0版本实践
  6. 腾讯、百度、阿里、微软面试题精选(不断更新)
  7. python你会吗_Python这些问题你会吗?
  8. 【转】Dicom基础知识
  9. 漫画:千万别在同事面前装逼
  10. 临界区设计太大或太小有何缺点_小户型太小怎么办?17个实用空间设计拯救你家,小家越住越大...
  11. 电脑录屏工具_这四款录屏工具,也许是电脑录屏软件中免费、无广告且最实用的,程序员必备...
  12. 20120510,OSPF第三部分
  13. 休闲娱乐的计算机配置,入门APU主机配置 1199元双核APU家用娱乐电脑配置推荐
  14. mysql cmake 编译出错_Mysql安装过程中CMAKE编译出错
  15. python函数参数冒号_Python3 - 参数中的冒号和箭头
  16. centos7重启后/etc/resolv.conf 被还原解决办法
  17. Docker技术入门与实战(第2版)2.5 本章小结
  18. java实现手机扫描二维码后网站跳转新页面
  19. steam加速_追梦加速器:Steam一周销量前十榜单,你的游戏排第几?
  20. Machine Learning: A Probabilistic Perspective——Chapter 1

热门文章

  1. 浅谈程序员接私单那点事及接私单需要注意的问题
  2. sun.net.ftp.FtpClient介绍
  3. 瑞星发布可防未知勒索病毒工具 将逐月公布更多漏洞
  4. 鼠标增强软件StrokeIt使用方法
  5. 清华大学高琪瑞:基于云端视觉计算建设全球先进的工科实验教学平台
  6. Java_实现身份证信息提取个人信息
  7. [目标跟踪]pysot和vot-toolkit的结合
  8. take credit for用法
  9. C/C++ 编程 —— OpenCV API 学习 笔记(更新中...)
  10. [转]SAP模块一句话入门