Reducing the fuel consumption is a major issue in the vehicle industry. In this paper, it is done by formulating a driving mission of a heavy-duty truck as an optimal control problem and solving it using dynamic programming. The vehicle model includes an engine and a gearbox with parameters based on measurements in test cells. The dynamic programming algorithm is solved by considering four specific types of transitions: transitions between the same gear, coasting in neutral gear, coasting with a gear engaged with no fuel injection and transitions involving gear changes. Simulations are performed on a driving cycle commonly used for testing distribution type of driving. In order to make sure that the truck does not deviate too much from a normal way of driving, restrictions on maximum and minimum allowed velocities are imposed based on statistics from real traffic data. The simulations show that 12.7 % fuel can be saved without increasing the trip time by allowing the truck to engage neutral gear and make small deviations from the reference trajectory.