Elimination(DP)

来源:互联网 发布:聚合数据看不懂 编辑:程序博客网 时间:2024/06/05 05:13

The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.

The elimination rounds are divided into main and additional. Each of the main elimination rounds consists ofc problems, the winners of the round are the firstn people in the rating list. Each of the additional elimination rounds consists ofd problems. The winner of the additional round is one person. Besides,k winners of the past finals are invited to the finals without elimination.

As a result of all elimination rounds at least n·m people should go to the finals. You need to organize elimination rounds in such a way, that at leastn·m people go to the finals, and the total amount of used problems in all rounds is as small as possible.

Input

The first line contains two integers c andd (1 ≤ c, d ≤ 100) — the number of problems in the main and additional rounds, correspondingly. The second line contains two integersn and m (1 ≤ n, m ≤ 100). Finally, the third line contains an integerk (1 ≤ k ≤ 100) — the number of the pre-chosen winners.

Output

In the first line, print a single integer — the minimum number of problems the jury needs to prepare.

Example
Input
1 107 21
Output
2
Input
2 22 12
Output
0
代码:;

#include<stdio.h>#include<algorithm>#include<string.h>#define MAX 100000using namespace std;int val[MAX];int w[MAX];int dp[MAX];int main(){    memset(dp,0,sizeof(dp));    int n,m,c0,d0;    int a,b,c,d;    scanf("%d%d%d%d",&n,&m,&c0,&d0);    int cnt=1,j;    int k=n/c0;    for(j=1;j<=k;j++){        val[cnt]=d0;        w[cnt++]=c0;    }    int i;    for(i=0;i<m;i++){        scanf("%d%d%d%d",&a,&b,&c,&d);        k=min(n/c,a/b);        for(j=1;j<=k;j++){           val[cnt]=d;           w[cnt++]=c;        }    }    for(i=1;i<cnt;i++){        for(j=n;j>=w[i];j--)            dp[j]=max(dp[j],dp[j-w[i]]+val[i]);    }    printf("%d\n",dp[n]);return 0;}


阅读全文
0 0