I'm afraid solving your problem in polynomial time is not possible unless P=NP. If one can solve your problem in polynomial time, then using this solution and binary search, it would be possible to solve the [Subset Sum problem][1] in polynomial time as well. But the latter problem is NP-complete. [1]: https://en.wikipedia.org/wiki/Subset_sum_problem