If we disregard potential constraints on the range of values of A and T, an optimal approach also depends on the relationship between n and m :
If 2n\times log(n) > m then the solution described on StackOverflow (with the complexity of O(n\times log(n) + m\times n) for partial sum sorting) works the best; otherwise your solution is better.