memory limit per test

512 megabytes

input

standard input

output

standard output

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 nspace-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

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

Examples
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<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#define MAXN 5009
typedef long long LL; using namespace std;
LL a[MAXN];
LL ans;
LL solve(LL l, LL r, LL bas)
{
if (l > r)
return ;
int p = min_element(a + l, a + r + ) - a;
//cout << min(r - l + 1, solve(l, p - 1, a[p]) + solve(p + 1, r, a[p]) + a[p] - bas) << endl;
return min(r - l + , solve(l, p - , a[p]) + solve(p + , r, a[p]) + a[p] - bas);
}
int main()
{
LL n;
while (scanf("%lld", &n) != EOF)
{
LL sum = ;
for (LL i = ; i < n; i++)
scanf("%lld", &a[i]),sum+=a[i];
printf("%lld\n",sum/n);
}
}

C. Painting Fence 分治的更多相关文章

  1. CF-448C Painting Fence 分治

    Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...

  2. codeforces 256 div2 C. Painting Fence 分治

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

  3. painting fence - 分治 - Codeforces 448c

    2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...

  4. 【题解】Painting Fence

    [题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...

  5. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  6. 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 ...

  7. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  8. Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目

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

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

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

随机推荐

  1. P2973 [USACO10HOL]赶小猪

    跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...

  2. php 文件上传限制修改

    修改PHP上传文件大小限制的方法 1. 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完.但在php中,默认的该页最久执行时间为 30 秒.就是说超过30秒,该脚本就停止 ...

  3. ASP.NET Core&EF 笔记

    首先创建Asp.net Core项目,然后通过 NuGet 安装 EntityFrameworkCore: Microsoft.EntityFrameworkCore.SqlServer Micros ...

  4. MySQL与MongoDB的操作对比

    MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方 ...

  5. 微信小程序组件解读和分析:三、swiper滑块视图

    swiper滑块组件说明: 滑块视图容器,用于展示图片,可以通过用户拖拽和设置自动切换属性控制图片的切换   组件的使用示例的运行效果如下: 下面是WXML代码: [XML] 纯文本查看 复制代码 ? ...

  6. Maven对不同的测试环境用不同的参数进行打包

    通过mvn package -P ${env} 加载不同配置文件 1.pom.xml中的配置 filter-dev.properties jdbc.properties

  7. MySQL详解(25)-----------MySQL性能优化

    1.    简介 在Web应用程序体系架构中,数据持久层(通常是一个关系数据库)是关键的核心部分,它对系统的性能有非常重要的影响.MySQL是目前使用最多的开源数据库,但是MySQL数据库的默认设置性 ...

  8. Android yuv转Bitmap

      YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null); if(image!=nu ...

  9. JS高级——文件操作

    https://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/12/2208589.html https://blog.csdn.net/pl16 ...

  10. Vue.js——router-link阻止click事件

    router-link 只能单纯做路由跳转 https://segmentfault.com/q/1010000007896386