inputs: I found an interesting problem on https://codeforces.com/problemset/problem/526/C:
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] 

One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units.

Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.


-----Input-----

The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≤ C, H_{r}, H_{b}, W_{r}, W_{b} ≤ 10^9).


-----Output-----

Print a single integer — the maximum number of joy units that Om Nom can get.


-----Examples-----
Input
10 3 5 2 3

Output
16



-----Note-----

In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.

I tried it in Python, but could not do it. Can you solve it?
targets: import sys
f = sys.stdin

C, Hr, Hb, Wr, Wb = map(int, f.readline().strip().split())

if Hr/Wr < Hb/Wb:
    Hr, Hb, Wr, Wb = Hb, Hr, Wb, Wr

if (C % Wr) == 0 and (C // Wr) > 0:
    print((C // Wr)*Hr)
    
elif (C // Wr) == 0:
    print((C // Wb)*Hb)

else:
    nmax =  (C // Wr)
    pmax = nmax*Hr + ((C - nmax*Wr) // Wb) * Hb
    dmax = ((C - (nmax-0)*Wr) % Wb)
    #print(0, pmax, dmax)
    
    #
    #pm1 = (nmax-1)*Hr + ((C - (nmax-1)*Wr) // Wb) * Hb 
    #if pm1>pmax:
    #    pmax = pm1
    if  Hr/Wr > Hb/Wb:
        dx = dmax * (Hb/Wb) / (Hr/Wr - Hb/Wb)    
    elif  Hr/Wr < Hb/Wb: 
        dx = 0 
    else:
        dx = Wb * Wr
        if Wr<Wb:
            nmax =  (C // Wb)
            pmax = nmax*Hb + ((C - nmax*Wb) // Wr) * Hr   
        if Wr>Wb:
            nmax =  (C // Wr)
            pmax = nmax*Hr + ((C - nmax*Wr) // Wb) * Hb   
            
    if Wr>Wb and dx>0:    
        for k in range(1, C//Wr):
            if k*Wr > dx:
                break
            pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb 
            dk = ((C - (nmax-k)*Wr) % Wb)
            #print(k, pmax, pk, dk)
            if pk>pmax:
                pmax = pk
            if dk==0 :
                break
    elif Wr<Wb and dx>0:   
        for j in range(1, C//Wb+1):
            k = nmax - (C-j*Wb)//Wr
            if k*Wr > dx:
                break
            
            pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb 
            dk = ((C - (nmax-k)*Wr) % Wb)
            #print(j, k, pmax, pk, dk, (nmax-k), ((C - (nmax-k)*Wr) // Wb) )
            if pk>pmax:
                pmax = pk
                #dmax = dk
            if dk==0 :
                break            
            
#    elif Wr<Wb and dx>0:   
#        for j in range(1, C//Wb+1):
#            k = (j*Wb - dmax)//Wr
#            if k*Wr > dx:
#                break
#            pk = (nmax-k)*Hr + ((C - (nmax-k)*Wr) // Wb) * Hb 
#            dk = ((C - (nmax-k)*Wr) % Wb)
#            print(j, k, pmax, pk, dk, (nmax-k), ((C - (nmax-k)*Wr) // Wb) )
#    ...