program main implicit none integer*8::N,K,atari,buy,total character*50::S integer::i data atari/0/,buy/0/ read *,N,K read *,S(1:N) do i=1,MIN(N,K) if(atari.eq.0) then buy = buy + 1 else atari = atari - 1 end if atari = atari + ICHAR(S(i:i))-ICHAR('0') end do if(N.gt.K .or. atari.ge.buy) then print '(i0)', buy return end if ! print *,"first:", buy, atari if((K-N-1)/N.ge.1) then total = buy + (buy - atari) * (K-N-1)/N else total = buy end if ! print *,"second:", total, (K-N-1)/N buy = 0 do i=1,(K-((K-N-1)/N+1)*N) if(atari.eq.0) then buy = buy + 1 else atari = atari - 1 end if atari = atari + ICHAR(S(i:i))-ICHAR('0') end do total = total + buy print '(i0)', total end program main