A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a worker is hired, he will get the salary even if he is not working. The manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. Then the manager will confront such a problem: how many workers he will hire or fire each month in order to keep the lowest total cost of the project. 

Input

The input may contain several data sets. Each data set contains three lines. First line contains the months of the project planed to use which is no more than 12. The second line contains the cost of hiring a worker, the amount of the salary, the cost of firing a worker. The third line contains several numbers, which represent the minimal number of the workers needed each month. The input is terminated by line containing a single '0'. 

Output

The output contains one line. The minimal total cost of the project. 

Sample Input

3 
4 5 6
10 9 11
0

Sample Output

199

对于一组数据雇佣金,工资,解雇金一定,那么对于每一个子问题,只要考虑前一月的留下的人是否满足需求,如果满足就解雇差值,反之如果不够就解雇差值,所以这就要求dp初始化为雇佣对应月份的人数。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define mm(a,n) memset(a,n,sizeof(a))
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int dp[13][10005],moth[13];
int main()
{
    int n,hire,salary,fire,maxx=0;
//	            freopen("C:\\Users\\nuoyanli\\Desktop\\acminput.txt","r",stdin);
//    freopen("C:\\Users\\nuoyanli\\Desktop\\acmoutput.txt","w",stdout);
    while(cin>>n&&n)
    {
        mm(dp,inf);
        cin>>hire>>salary>>fire;
        for(int i=1; i<=n; i++)
        {
            cin>>moth[i];
            if(moth[i]>maxx)
                maxx=moth[i];
        }
        for(int i=1; i<=maxx; i++)
            dp[1][i]=(salary+hire)*i;
        int mmm=inf;
        for(int i=2; i<=n; i++)
            for(int j=moth[i]; j<=maxx; j++)
                for(int k=moth[i-1]; k<=maxx; k++)
                        dp[i][j]=min(dp[i][j],(j>k?hire*(j-k):fire*(k-j))+salary*j+dp[i-1][k]);
        for(int i=moth[n]; i<=maxx; i++)
            mmm=min(mmm,dp[n][i]);
        cout<<mmm<<endl;
    }
    return 0;
}

 希望对读者有所帮助。(由于笔者技术有限如有错误欢迎评论区指正)