C. Painting Fence

Bizon the Champion isn't just attentive, he also is very hardworking.

Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.

Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.

Input

The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the minimum number of strokes needed to paint the whole fence.

Sample test(s)
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
Note

In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.

In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.

In the third sample there is only one plank that can be painted using a single vertical stroke.

 #include <iostream>

 using namespace std;

 int main() {
int n; cin >> n;
int a[n+];
a[] = ;
for (int i=; i<=n; i++) cin >> a[i];
int dp[n+][n+];
for (int j=; j<=n; j++) dp[n][j] = ;
for (int i=n-; i>=; i--) for (int j=; j<=i; j++) {
if (a[j] >= a[i+]) dp[i][j] = dp[i+][i+]; //Already painted
else {
dp[i][j] = min( + dp[i+][j], a[i+]-a[j] + dp[i+][i+]);
}
}
cout << dp[][] << endl;
}

Codeforces Round #256 (Div. 2) C. Painting Fence的更多相关文章

  1. Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

    题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...

  2. Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  3. Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)

    [题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...

  4. Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

    解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...

  5. 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs

    题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...

  6. Codeforces Round #256 (Div. 2) 题解

    Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #256 (Div. 2)

    A - Rewards 水题,把a累加,然后向上取整(double)a/5,把b累加,然后向上取整(double)b/10,然后判断a+b是不是大于n即可 #include <iostream& ...

  8. Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)

    转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...

  9. Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)

    题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...

随机推荐

  1. JavaScript 简单吗

    英文:Aurélien Hervé  译文:众成翻译/msmailcode 这里有一些 Javascript初学者应该知道的技巧和陷阱.如果你已经是专家了,顺便温习一下. Javascript也只不过 ...

  2. samba和SELINUX 冲突

    在使用Samba进行建立Window与Linux共享时,要是不能访问,出现“您可能没有权限使用网络资源”. setsebool -P samba_enable_home_dirs on setsebo ...

  3. mariadb/mysql使用Navicat连接报错

    [问题1] 使用Navicat连接服务器的mariadb/mysql时报错 access denied for user root@192.168.xx.xx(using password:yes) ...

  4. 决策树和adaboost

    前面:好老的东西啊,啊啊啊啊啊啊啊啊啊 来源于统计学习方法: 信息增益: 其中 信息增益率: 基尼指数: 取gini最小的 先剪枝——在构造过程中,当某个节点满足剪枝条件,则直接停止此分支的构造. 后 ...

  5. 使用vs2015编辑c++模板程序报错2019

    笔者这几天在熟悉vs2015工具编辑c++,(从前用的都是vc++6.0).发现还真是不容易使用,简单的一个小程序会报错一大堆你看不懂的问题.现将我发现的重要的一个问题呈现给大家. 在使用类模板设计对 ...

  6. 浮动元素垂直居中,bootstrap栅格布局垂直居中

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 树莓派3B更换源为阿里源

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #科大源 sudo nano /etc/apt/sources.list deb htt ...

  8. springboot 1.5.X junit测试

    import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; impo ...

  9. Spark(九)Spark之Shuffle调优

    一.概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作.因此,如果要让作业的性能更上一层楼,就有必要对shuffle过程进行 ...

  10. LoadRunner FAQ

    LoadRunner FAQ web_concurrent_start和web_concurrent_end web_concurrent_start 语法: int web_concurrent_s ...