Fence(CF-324F)

Problem Description

Vasya should paint a fence in front of his own cottage. The fence is a sequence of nwooden 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 unattractivenessvalue. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness 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,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.

Input

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.

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.

Examples

Input

4
5 7
3 3 4 1

Output

3

Input

3
2 3
1 3 1

Output

2

Input

3
3 3
2 2 2

Output

-1

题意:现在有 n 个栅栏要刷漆,每个栅栏只能刷红色、绿色的一种颜色,最多使用颜色的面积为 x、y,现在给出 n 个栅栏的大小,如果相邻栅栏颜色不同,那么会产生一个等于不同部分面积的权值,求最小权值,如果无法获得最小权值,输出 -1

思路:三维 DP

设 dp[i][j][k] 代表对前 i 个栅栏来说,使用颜色为 k 时,这 i 个栅栏使用颜色为红色面积为 j 时的最小代价

由于只能染色红色或绿色,那么 k 的取值为 0、1,于是有:

  • dp[i][j][0]=min(dp[i-1][j-a[i]][0],dp[i-1][j-a[i]][1]+min(a[i],a[i-1]) )
  • dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+min(a[i],a[i-1]) )

需要注意的是,由于第二维是以红色面积为基准的,但不要忘记绿色的限制值,由于栅栏的面积是固定的,因此可利用前缀和来统计栅栏的面积,这样一来,只要知道了红色的面积,那么绿色的面积也就知道了

此外,注意本题要使用文件读写

Source Program

#include<bits/stdc++.h>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int a[N];
int sum[N];
int dp[200+5][40000+5][2];
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    int n;
    int x,y;
    scanf("%d",&n);
    scanf("%d%d",&x,&y);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum[i]=sum[i-1]+a[i];
    }

    memset(dp,INF,sizeof(dp));
    if(a[1]<=x)
        dp[1][a[1]][0]=0;
    if(a[1]<=y)
        dp[1][0][1]=0;
    for(int i=2;i<=n;i++){//i个栅栏
        for(int j=0;j<=x;j++){//使用红色颜色面积
            int sub=min(a[i],a[i-1]);//相邻栅栏的代价
            if(sum[i]-j<=y){//使用绿色的面积不超过y时
                dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+sub);
                if(a[i]<=j)//使用红色面积不超过j时
                    dp[i][j][0]=min(dp[i-1][j-a[i]][0],dp[i-1][j-a[i]][1]+sub);
            }
        }
    }

    int minn=INF;
    for(int i=0;i<=x;i++){
        minn=min(minn,dp[n][i][0]);
        minn=min(minn,dp[n][i][1]);
    }

    if(minn==INF)
        printf("-1\n");
    else
        printf("%d\n",minn);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值