If we go t1 seconds with acceleration a1 and then t2 seconds with acceleration a2, our final speed will be the same as if we swapped these two segments and first went t2 seconds with acceleration a2 and only then t1 seconds with acceleration a1. Which is better in terms of the total distance covered? WLOG we can assume that the initial speed is 0. In the first case we will travel a1*t1^2/2 + a1*t1*t2 + a2*t2^2/2. In the second case the formula is almost the same, only the middle term changes to a2*t1*t2. Conclusion: in the optimal solution the segments have to be ordered by acceleration in descending order. Now, note that for each fuel tank the two possible accelerations are inverses of each other: if one is a, the other is 1/a. If you order the fuel tanks according to the larger of those two accelerations, you now know that the first fuel tank will allow you to either accelerate most or accelerate least. In other words, in the optimal solution the first fuel tank will either be used first or last. And so on. That is starting to smell like dynamic programming... but what exactly are the states? One could assume that we just find the optimal solution for tanks 2 through N, and then decide whether to append or prepend fuel tank 1. (This would actually be a greedy solution, there is no need to memoize anything here.) However, this approach does not work. If we decide to start with tank 1, we might want to use the other tanks in a different order than if we decided to end with tank 1. Instead, think of the problem as follows: Imagine a plot with car speed on the y axis and elapsed time on the x axis. The goal is to maximize the distance traveled -- i.e., the area below the curve that represents the acceleration of your car. In this plot, each fuel tank will show as a straight line -- either as (+x[i],+y[i]), or as (+y[i],+x[i]). Let S = sum x[i] + sum y[i]. Note that for the given constraints S is a small integer. Regardless of how we choose to use the fuel tanks, the plot of speed vs. time for our car will end in some point (i,S-i). We can now try all possible values of i, find the best solution for each of them, and take the maximum of those. (Many values of i might be unreachable, in which case we assume that the best solution is zero.) And that brings us to the DP formulation of our solution. First, we sort and re-number the fuel tanks as described above. Now, let S[k] = sum_{i >= k} x[i]+y[i]. We know that if we only used the fuel tanks k..N, the plot would end in some point (i,S[k]-i). Let dp[k][i] be the largest area under a curve that corresponds to the fuel tanks k..N and ends in the point (i,S[k]-i). If we know all values dp[k][i] for some k, we can use them to compute all values dp[k-1][i]: when computing dp[k-1][i] we will try whether to use tank k-1 first or last, and in both cases we look at some dp[k][j] to see what's the best way to order the remaining fuel tanks. This gives us a pseudo-polynomial-time solution that runs in O(N*log N + N*(sum x[i]+sum y[i])), which is fast enough and fits into available memory.