The OP remarked in a comment that he found the problem tagged as "brute force and DP". Which is absolutely ridiculous. Here's the linear time solution:
Observe that the order of a, b, c is quite irrelevant, so sort them to make a ≥ b ≥ c.
Calculate g = gcd (a, b, c) (greatest common divisor). If n is not divisible by g then there is no solution. Otherwise, divide a, b, c and n by g. Now gcd (a, b, c) = 1.
Calculate g = gcd (b, c), which means n - ai must be a multiple of g. Find i0 = the smallest i such that n - ai is a multiple of g. Such an i exists because gcd (a, g) = 1. Possible values for i that can lead to solutions are i0, i0 + g, i0 + 2g and so on. If n - a*i0 < 0 then there is no solution.
Divide b and c by g. We now have to find j, k that solve (n - ai)/g = bj + ck, for i = i0, i0 + g, i0 + 2g and so on. gcd (b, c) is now 1. For each of those i, let m = (n - ai) / g, then we need bj modulo c = m modulo c. Since gcd (b, c) = 1, there is a smallest j0 ≥ 0 solving this equation. The other values j solving this would be j0 + c, j0 + 2c, j + 3c. However, every time we increase j by c we need to decrease k by b, and since b ≥ c this cannot improve the solution. We have k = (m - b * j0) / c, and this k must be ≥ 0. Increasing j cannot make k ≥ 0 if it was less than 0 for a smaller j. This means that for a given i, we choose j = smallest j such that bj modulo c = m modulo c, and k = (m - b * j0) / c.
To find j0 quickly, we create a lookup table T mapping a modulo value m to the smallest j with bj modulo c = m. This is done in linear time by calculating b*0 modulo c, b*1 modulo c etc. and setting T [b*t modulo c] = t for 0 ≤ t < c.
The algorithm now starts with i = i0, and no solution found so far. If n - ai < 0 then return with the best solution found so far. Otherwise let m = (n - ai) / g. Let j = T [m modulo c], k = (m - b*j) / c. If k ≥ 0 and i + j + k is better than the best solution so far then we found a new best solution. Otherwise continue with i replaced with i + g.
We speed up the algorithm by observing that after calculating m, we know that i + j + k ≤ i + m / c which is never increasing. So if we found a solution and i + m / c is not greater than that solution then we can exit the algorithm because we know we found a solution that cannot be improved any further.
With the input n = 4000, a, b, c = 1, 2, 3: We rearrange a = 3, b = 2, c = 1. gcd (a, b, c) = 1 so a, b, c, n remain unchanged. g = gcd (b, c) = 1, so b, c are unchanged, the table T contains one value T [0] = 0.
We let i = 0, and n - ai is divisible by g = 1. So we start with i = 0, m = 4000, j = T [0] = 0, k = (4000 - j*b) / c = 4000, giving the solution i=0,j=0,k=4000.
Next we let i = 1, m = 3997. i + m / c = 3998 < 4000, so the solution that we found earlier is optimal. So the problem that exceeded the time limit is actually so simple that I can write down the complete calculation. (BTW if c = 1 after dividing a, b, c, n by gcd (a, b, c), then i = 0, j = 0, k = n, is always the optimal solution).
I think the solution might be sub-linear in time if we find i0 faster O (g) by using Euclid's algorithm, and instead of building a table T in O (c) find the values j using Euclid's algorithm, hoping that we can prove that quickly that a solution found is optimal.