# 题目

## Description

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.

3
4 5 6
10 9 11
0

199

# 题解

`dp[i][j]` 来记录第 `i` 个月雇佣 `j` 个工人需要的最少的钱数

# 代码

```/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい？
エリーチカ！

*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;

const int INF = 0x7FFFFFFF / 2;
const int maxn = 13;
const int maxp = 10000;

int p[maxn];
int dp[maxn][maxp];

int n;
int h,s,f;

int GetMoney(int ss,int vv) {
if(ss > vv)
return (ss - vv) * f + vv * s;
return (vv - ss) * h + vv * s;
}

bool Do() {
scanf("%d",&n);
if(n == 0)
return false;
scanf("%d%d%d",&h,&s,&f);

int mp = 0;
for(int i = 1;i <= n;i++) {
scanf("%d",&p[i]);
mp = max(mp,p[i]);
}
p = 0;

memset(dp,-1,sizeof(dp));
dp = 0;

for(int i = 1;i <= n;i++)
for(int j = p[i];j <= mp;j++) {
int Min = INF;
for(int k = p[i - 1];k <= mp;k++)
if(dp[i - 1][k] != -1)
Min = min(Min,dp[i-1][k] + GetMoney(k,j));

dp[i][j] = Min;
}

int ans = INF;
for(int i = 1;i <= mp;i++)
if(dp[n][i] != -1)
ans = min(ans,dp[n][i]);

printf("%d\n",ans);

return true;
}

int main() {
while(Do());
return 0;
}
```