program main implicit none character::c(15) integer*8::N,i integer*8::num(0:9),num2(0:9),plus,minus,char integer*8::minvalue,maxvalue data num/10*0/,plus/0/,minus/0/,minvalue/0/,maxvalue/0/ read *,N read *,c(1:N) do i=1,N if(c(i).eq.'+') then plus = plus + 1 else if(c(i).eq.'-') then minus = minus + 1 else char= ICHAR(c(i))-ICHAR('0') num(char) = num(char) + 1 end if end do num2 = num do i=1, minus maxvalue = maxvalue - get_min_num(num) end do do i=1, plus maxvalue = maxvalue + get_min_num(num) end do do i=1, N-(minus+plus)*2 maxvalue = maxvalue + get_min_num(num)*10**(i-1) end do if(minus.eq.0) then do i=1, plus minvalue = minvalue + get_max_num(num2) end do do i=1, N-2*plus-num2(0) minvalue = minvalue + get_max_num(num2)*10**(i-1) end do else do i=1, plus+1 minvalue = minvalue + get_min_num(num2) end do do i=1, minus-1 minvalue = minvalue - get_min_num(num2) end do do i=1, N-(minus+plus)*2 minvalue = minvalue - get_min_num(num2)*10**(i-1) end do endif print '(i0," ",i0)',maxvalue, minvalue contains integer function get_max(num, plus, minus, N) result(maxvalue) integer*8::N,i integer*8::num(10),plus,minus integer*8::digitnum,maxdigit digitnum = N - plus - minus maxdigit = digitnum - plus - minus maxvalue = 0 do i=1,minus do end do end do end function get_max function get_min_num(num) result(v) integer*8::num(0:9) integer*8::i, v do i=0, 9 if(num(i).ne.0) then num(i) = num(i)-1 v = i return end if end do end function get_min_num function get_max_num(num) result(v) integer*8::num(0:9) integer*8::i, v do i=9, 0, -1 if(num(i).ne.0) then num(i) = num(i)-1 v = i return end if end do end function get_max_num end program main