Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1, 2, ..., n from left to right. The height of the i-th board is hi centimeters.
Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over a square centimeters of the fence red, and he can not paint over b square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors.
In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. Theunattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely.
![](https://i-blog.csdnimg.cn/blog_migrate/16606d00c1bbf587f698ebe62ccbd0f0.png)
The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6.
The first line contains a single integer n (1 ≤ n ≤ 200) — the number of boards in Vasya's fence.
The second line contains two integers a and b (0 ≤ a, b ≤ 4·104) — the area that can be painted red and the area that can be painted green, correspondingly.
The third line contains a sequence of n integers h1, h2, ..., hn (1 ≤ hi ≤ 200) — the heights of the fence boards.
All numbers in the lines are separated by single spaces.
Print a single number — the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print - 1.
4 5 7 3 3 4 1
3
3 2 3 1 3 1
2
3 3 3 2 2 2
-1
/**
题目大意:某人要用red和green两种染料刷墙,问怎么样才能让两种颜色的连接数量最少?
给定red颜色的数量a和green颜色的数量b,还有每面墙的高度。
要求是,每一面墙只能用一种颜色刷,并且要刷满,不能只刷一部分。如果没有能够满足条件的刷法。输出-1
思路:动态规划
每一面墙可以用两种颜色刷,但是每种颜色有数量限制,并且还要求两种颜色连接数最少,用动态规划解决最优化。
dp[i][j][s]表示前i面墙刷了j面red颜色并且最后一面为s颜色时,两种颜色的连接数最少数量,s为0或1,表示red和green。
初始化dp为无穷大,但显然应该有dp[0][0][0]=0,dp[0][0][1]。
当我们刷完第i面墙时,我们需要枚举到目前为止0颜色的数量(也就是枚举1颜色,除了0就是1),并考虑第i+1面应该刷什么,所以:
状态转移:
1, 首先讨论颜色0,
如果下一面刷 颜色0,若当前刷的0总数量不大于总数a(j+num[i+1]),则
dp[i+1][j+num[i+1]][0]=dp[i][j][0],否则,就不能把第i+1面墙刷成颜色0。
如果下一面刷颜色1,若当前刷的1总数量不大于总数b(sum[i+1]-j),则
dp[i+1][j][1]=dp[i][j][0]+min(num[i],num[i+1]),否则,就不能把第i+1面墙变成1。
2, 接着讨论颜色1,
如果下一面刷 颜色0,若当前刷的0总数量不大于总数b(j+num[i+1]),则
dp[i+1][j+num[i+1]][0]=dp[i][j][1]+min(num[i],num[i+1]),否则,就不能把第i+1面墙刷成颜色0。
如果下一面刷颜色1,若当前刷的1总数量不大于总数b(sum[i+1]-j),则
dp[i+1][j][1]=dp[i][j][1],否则,就不能把第i+1面墙变成1。
最终,枚举枚举dp[n][j][s],选择第n面墙中最小的那个dp,就是答案。
但是,如果最小值等于初始化的值,那说明没有可以满足条件的刷墙的方法,需要输出-1.
**/
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 202;
const int inf = ~0u>>2;//Max
int dp[N][N*N][2],num[N],sum[N];//dp,num[i]每一面的高度,sum[i]到第i面时的总高度
inline void chkmin(int &a, const int &b)//简化赋值
{
if(a>b)a=b;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,a,b;
/**输入**/
cin>>n>>a>>b;
for(int i=1; i<=n; i++)
cin>>num[i];
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+num[i];
/**初始化**/
for(int i = 0; i <= n; i++)
for(int j = 0; j <=a; j++)
for(int p = 0; p < 2; p++)
dp[i][j][p] = inf;
dp[0][0][0] = 0;
dp[0][0][1] = 0;
/**状态转移**/
for(int i = 0; i < n; i++)
for(int j = 0; j <= a; j++)
{
if(dp[i][j][0] != inf)
{
int v = dp[i][j][0];
if(j + num[i+1] <= a)//第i+1面墙刷颜色0
chkmin(dp[i+1][j+num[i+1]][0],v);
if(sum[i+1] - j <= b)//第i+1面墙刷颜色1
chkmin(dp[i+1][j][1],v+min(num[i],num[i+1]));
}
if(dp[i][j][1] != inf)
{
int v = dp[i][j][1];
if(j + num[i+1] <= a)//第i+1面强刷颜色0
chkmin(dp[i+1][j+num[i+1]][0],v+min(num[i],num[i+1]));
if(sum[i+1] - j <= b)//第i+1面墙刷颜色1
chkmin(dp[i+1][j][1],v);
}
}
int ans = inf;
for(int i = 0; i <=a ; i++)
for(int j = 0; j < 2; j++)
chkmin(ans,dp[n][i][j]);//寻找最大值,即答案
if(ans == inf)
cout<< -1 << endl;
else
cout<< ans << endl;
}