Problem:
Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
#define N 1000002
using namespace std;
struct object{
	int c,w,id;
}a[N];
int head[N],ver[N*2],nxt[N*2],l;
int n,x,d,s,i,j,m[N],p[N],c[N],w[N],ncnt,nc[N],nw[N],f[N],ans;
int read()
{
	char c=getchar();
	int w=0;
	while(c<'0'||c>'9') c=getchar();
	while(c<='9'&&c>='0'){
		w=w*10+c-'0';
		c=getchar();
	}
	return w;
}
void insert(int x,int y)
{
	l++;
	ver[l]=y;
	nxt[l]=head[x];
	head[x]=l;
}
void dfs(int x)
{
	c[x]=m[x];w[x]=1;
	for(int i=head[x];i;i=nxt[i]){
		int y=ver[i];
		dfs(y);
		c[x]+=c[y];w[x]+=w[y];
	}
}
int my_comp(const object &a,const object &b)
{
	return a.w*b.c>b.w*a.c;
}
signed main()
{
	n=read();x=read();d=read();
	m[1]=read();
	for(i=2;i<=n;i++){
		m[i]=read();p[i]=read();
		insert(p[i],i);
	}
	dfs(1);
	s=min(n,d);
	for(i=1;i<=n;i++){
		int k;
		for(k=1;s-(1<<k)+1>0;k++){
			ncnt++;
			nc[ncnt]=(1<<(k-1))*c[i];
			nw[ncnt]=(1<<(k-1))*w[i];
		}
		k--;ncnt++;
		nc[ncnt]=(s-(1<<k)+1)*c[i];
		nw[ncnt]=(s-(1<<k)+1)*w[i];
	}
	memset(f,0x3f,sizeof(f));
	f[0]=0;
	for(i=1;i<=ncnt;i++){
		for(j=ncnt*n;j>=nw[i];j--) f[j]=min(f[j],f[j-nw[i]]+nc[i]);
	}
	for(i=1;i<=n;i++) a[i].c=c[i],a[i].w=w[i],a[i].id=i;
	sort(a+1,a+n+1,my_comp);
	for(i=1;i<=ncnt*n;i++){
		int v=x-f[i],sum=i;
		if(v<0) continue;
		for(j=1;j<=n;j++){
			int cnt=(a[j].id==1)?x/c[1]:d;
			if(cnt-s>0){
				if(v-(cnt-s)*a[j].c>=0) sum+=(cnt-s)*a[j].w,v-=(cnt-s)*a[j].c;
				else{
					sum+=(v/a[j].c)*a[j].w;
					break;
				}
			}
		}
		ans=max(ans,sum);
	}
	printf("%lld\n",ans);
	return 0;
}