Lets call the distance between a and the school 'x'

so the total distance traveled = 300x + 700(5-x)

we want to minimize the function y = 300x + 700(5-x)

which simplifies to

y = 300x + 3500 - 700x

y = -400x + 3500

To minimize the function we need to maximize x, x=5 is the highest number that makes sense.

therefore the school should be in town B.