Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' .
for ( $ i = 0 ; $ i < $ n and $ arr [ $ i ] <= $ res ; $ i ++ ) $ res = $ res + $ arr [ $ i ] ; return $ res ; }