program twin_and_ox_game
implicit none
integer :: b(2,3), c(3,2), g(3,3)
integer :: bro, sis
b = 0
c = 0
read(*,*) b(1,1), b(1,2), b(1,3)
read(*,*) b(2,1), b(2,2), b(2,3)
read(*,*) c(1,1), c(1,2)
read(*,*) c(2,1), c(2,2)
read(*,*) c(3,1), c(3,2)
g = 0
bro = 0
sis = 0
call get_score(b,c,g,bro,sis,0)
write(*,'(i0)') bro
write(*,'(i0)') sis
stop
contains
recursive subroutine get_score(b,c,g,bro,sis,n)
implicit none
integer, intent(in) :: b(2,3), c(3,2), g(3,3), n
integer, intent(inout) :: bro, sis
integer :: i, j, s, h(3,3), ix, jx
integer :: tbro, tsis
if (n.eq.9) then
bro = 0
sis = 0
do i = 1, 2
do j = 1, 3
if ((g(i,j).ne.0).and.(g(i,j).eq.g(i+1,j))) then
bro = bro + b(i,j)
else
sis = sis + b(i,j)
end if
if ((g(j,i).ne.0).and.g(j,i).eq.g(j,i+1)) then
bro = bro + c(j,i)
else
sis = sis + c(j,i)
end if
end do
end do
return
end if
if (mod(n,2).eq.0) then
s = -2000000
do i = 1, 3
do j = 1, 3
if (g(i,j).eq.0) then
h = g
h(i,j) = 1
call get_score(b,c,h,tbro,tsis,n+1)
if (tbro-tsis.gt.s) then
s = tbro-tsis
ix = i
jx = j
end if
end if
end do
end do
h = g
h(ix,jx) = 1
call get_score(b,c,h,bro,sis,n+1)
else
s = 2000000
do i = 1, 3
do j = 1, 3
if (g(i,j).eq.0) then
h = g
h(i,j) = -1
call get_score(b,c,h,tbro,tsis,n+1)
if (tbro-tsis.lt.s) then
s = tbro-tsis
ix = i
jx = j
end if
end if
end do
end do
h = g
h(ix,jx) = -1
call get_score(b,c,h,bro,sis,n+1)
end if
return
end subroutine get_score
end program twin_and_ox_game