Codeforces 234 F. Fence
DP
Dp [I] [j] [k] In Front Of I railing, j are painted as red, and the current railing color is 0 red 1 green
F. Fencetime limit per test2 secondsmemory limit per test256 megabytesinputinput.txtoutputoutput.txt
Vasya shoshould paint a fence in front of his own cottage. The fence is a sequenceNWooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1 ,? 2 ,?...,?NFrom left to right. The height ofI-Th board isHICentimeters.
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 overASquare centimeters of the fence red, and he can not paint overBSquare centimeters green. Each board of the fence shocould 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 shocould 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.
The picture shows the fence, where the heights of boards (from left to right) are 2, 3, 4, 3, 1. the first and the second th boards are painted red, the others are painted green. the first and the second boards have contact length 2, the fourth and second th boards have contact length 3, the second th and the sixth have contact length 1. therefZ enabled? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + keys/odw/PGVtPm48L2VtPj + keys =" s fence.
The second line contains two integersAAndB(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 sequenceNIntegersH1 ,?H2 ,?...,?HN(1? ≤?HI? ≤? (200)-the heights of the fence boards.
All numbers in the lines are separated by single spaces.
Output
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.
Sample test (s) input
45 73 3 4 1
Output
3
Input
32 31 3 1
Output
2
Input
33 32 2 2
Output
-1
#include
#include
#include
#include using namespace std;const int INF=(1<<30);int n,a,b,sum;int h[222],dp[2][40040][2];int main(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);scanf("%d%d%d",&n,&a,&b);for(int i=1;i<=n;i++){scanf("%d",h+i);sum+=h[i];}memset(dp[0],-1,sizeof(dp[0]));dp[0][0][1]=dp[0][0][0]=0;int now=1,pre=0;for(int i=1;i<=n;i++,swap(now,pre)){memset(dp[now],-1,sizeof(dp[now]));int mi=min(h[i],h[i-1]);for(int j=0;j<=a;j++){int c=j+h[i];if(dp[pre][j][0]!=-1){if(dp[now][c][0]==-1||dp[pre][j][0]