I am using correct approach. Still getting WA.
I am just taking the maximum ration and add all the ingredient according to it.
I am also trying to check all the edge cases but still i am getting wrong answer.

question link - http://www.spoj.com/problems/BYECAKES/

BYECAKES - Bye Bye Cakes

John is moving to a different city and he wants to use all his perishable food before doing it, to avoid wasting. Luckily all he has now is eggs, flour, sugar and milk, so he is going to make his famous cakes and give them to his friends as a goodbye gift. John only knows how to make an entire cake and not half a cake, a third of a cake, or any other portion. So, he will buy whatever is needed of each ingredient so that he can make an integer number of cakes and have nothing left. Of course, he wants to spend as little money as possible. You must help John to decide how much he should buy of each ingredient.

```
#include <bits/stdc++.h>
using namespace std;
struct data{
int rt, rm, val;
}arr[5];
data find_mx() {
data mx;
mx.rt = -1;
if(arr[0].rt > mx.rt){
mx.rt = arr[0].rt;
mx.rm = arr[0].rm;
}
if(arr[1].rt > mx.rt){
mx.rt = arr[1].rt;
mx.rm = arr[1].rm;
}
if(arr[2].rt > mx.rt){
mx.rt = arr[2].rt;
mx.rm = arr[2].rm;
}
if(arr[3].rt > mx.rt){
mx.rt = arr[3].rt;
mx.rm = arr[3].rm;
}
return mx;
}
int main() {
while(1) {
int A, B, C, D, a, b, c, d;
scanf("%d %d %d %d %d %d %d %d", &A, &B, &C, &D, &a, &b, &c, &d);
if(A == -1)
break;
arr[0].rt = A / a;
arr[0].rm = A % a;
arr[0].val = a;
arr[1].rt = B / b;
arr[1].rm = B % b;
arr[1].val = b;
arr[2].rt = C / c;
arr[2].rm = C % c;
arr[2].val = c;
arr[3].rt = D / d;
arr[3].rm = D % d;
arr[3].val = d;
data mx = find_mx();
if(mx.rm != 0)
mx.rt++;
for(int i = 0;i < 4;i++)
printf("%d ", ((mx.rt - arr[i].rt) * arr[i].val) - arr[i].rm);
printf("\n");
}
return 0;
```

}`