program main implicit none character*10::S character,allocatable::SS(:) integer::len,i,j,head,tail integer*8::num(5),opn,maxnum data opn/0/,maxnum/-100000000/ read *,S len = LEN_TRIM(S) allocate(SS(len)) call string2array(S,SS) do i=1,len SS = CSHIFT(SS,1) call array2string(S,SS) if(is_valid(S).eqv..true.)then num = 0 opn = 0 head = 1 do j=2,len-1 if(S(j:j).eq.'+'.or.S(j:j).eq.'-') then tail = j - 1 opn = opn + 1 read(S(head:tail),*) num(opn) head = j end if end do tail = len opn = opn + 1 read(S(head:tail),*) num(opn) maxnum = MAX(maxnum, SUM(num(1:opn))) end if end do print '(i0)', maxnum contains logical function is_valid(S) result(l) character*10::S integer::m m = LEN_TRIM(S) if(S(1:1).ne.'+'.and.S(1:1).ne.'-' .and. & S(m:m).ne.'+'.and.S(m:m).ne.'-') then l = .true. else l = .false. end if end function is_valid subroutine string2array(S,SS) implicit none character::SS(:) character*10::S integer::i do i=1,LEN_TRIM(S) SS(i) = S(i:i) end do end subroutine string2array subroutine array2string(S,SS) implicit none character::SS(:) character*10::S integer::i do i=1,LEN_TRIM(S) S(i:i) = SS(i) end do end subroutine array2string end program main