Why to take everything long long? problem in code

as n,m,x,y are all less than 10^9, they can be stored as int. i took only have and cost to be long long int in my code and it was not working fine.
but when i took everything to be long long int, it worked fine.
acc to me, if integer type variables are accumulating in a variable, only that should be taken as long long int. so, here only ‘have’ and ‘cost’ should be int.
link to my code -

correct output with all long long type - scholar1dobt

incorrect output -

check this small program


hope this clear ur doubt

canu explain what is happening here? what i observed is “the operation performed on two integer type variables was computed to be an integer and then stored into long long type variable. so, thats why it was negative.” refer to the code suggested by u in your answer.

yup it happeing like this