spoj SUMFOUR

can anyone optimize this code.giving tle.i want to do it using stl only
answer-
question-http://www.spoj.com/problems/SUMFOUR/

As you are trying to generate all the combination of summation and looping it through it is causing the TLE , your approach is correct but you need to be little bit smarter in searching the element in the multiset which will give the AC :slight_smile: Just remember in worst case the summation of A and B set or C and D set can give 4000 * 4000 elements which will be equal to 16 * 10 ^ 6 and then looping through the both may lead to to way beyond 10^9 loop …I have given enough hint you to go ahead think more :slight_smile: :slight_smile: remember smarted search