I have used the jacobsthal number it is a linear recurrence relation T(n)=T(n-1) + 2*T(n-2) T(0)=0,T(1)=1
this is a recurence realtion which I found is useful in finding soln
if n&1
// for ith cow
result =(b-1)cow[i] + b(sum_of_all -cow[i])
else
result =(b+1)cow[i] + b(sum_of_all -cow[i])