program prob6
    implicit none
    integer(8) :: N, i, j, K, L, m, o, tmp, s, ind, indd
    integer(8), allocatable :: a(:)
    character(40) :: x, y
    logical :: check

    read(*,*) N
    allocate(a(N))

    read(*,*) a

    K = 0
    L = 0
    do i = 1, N
        if(a(i) > 0)then
            K = K + 1
        else if(a(i) < 0)then
            L = L + 1
        end if
    end do

    s = 0
    do i = 1, N
        s = s + abs(a(i))
    end do

    if(K == N) then
        m = 100000
        do i = 1, N
            if(m > a(i)) then
                m = a(i)
                ind = i
            end if
        end do
        write(x,*) s - 2 * m
        write(*,'(a)') trim(adjustl(x))

        do i = 1, N
            if(i /= ind) then
                indd = i
                exit
            end if
        end do

        tmp = m
        do j = 1, N
            if(j == indd .or. j == ind) then
                cycle
            end if
            write(x,*) tmp
            write(y,*) a(j)
            write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
            tmp = tmp - a(j)   
        end do
        write(x,*) a(indd)
        write(y,*) tmp
        write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        tmp = a(indd) - tmp   

        ! if(mod(N, 2) == 1) then
        !     tmp = m
        !     do i = 1, N
        !         if(i == ind) then
        !             cycle
        !         end if
        !         write(x,*) a(i)
        !         write(y,*) tmp
        !         write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !         tmp = a(i) - tmp                 
        !     end do
        ! else
        !     tmp = m
        !     check = .false.
        !     do i = 1, N
        !         if(i == ind) then
        !             cycle
        !         end if
        !         if(check .eqv. .false.) then
        !             write(x,*) tmp
        !             write(y,*) a(i)
        !             write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !             tmp = tmp - a(i)                    
        !         else
        !             write(x,*) a(i)
        !             write(y,*) tmp
        !             write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !             tmp = a(i) - tmp 
        !         end if
        !     end do
        ! end if

    else if(L == N) then
        m = -100000
        do i = 1, N
            if(m < a(i)) then
                m = a(i)
                ind = i
            end if
        end do
        write(x,*) s + 2 * m
        write(*,'(a)') trim(adjustl(x))
        tmp = m
        do i = 1, N
            if(i == ind) then
                cycle
            end if
            write(x,*) tmp
            write(y,*) a(i)
            write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
            tmp = tmp - a(i)  
        end do

    else
        do i = 1, N
            if(a(i) <= 0) then
                m = a(i)
                ind = i
                exit
            end if
        end do
        do i = 1, N
            if(a(i) >= 0) then
                o = a(i)
                if(o == 0 .and. i == ind) then
                    cycle
                end if
                indd = i
                exit
            end if
        end do
        write(x,*) s
        write(*,'(a)') trim(adjustl(x))
        
        tmp = m
        do j = 1, N
            if(a(j) > 0) then
                    if(j == indd) then
                        cycle
                    end if
                    write(x,*) tmp
                    write(y,*) a(j)
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = tmp - a(j)                
            end if
        end do
        write(x,*) a(indd)
        write(y,*) tmp
        write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        tmp = a(indd) - tmp
        do j = ind + 1, N
            if(a(j) <= 0) then
                    if(j == indd) then
                        cycle
                    end if
                    write(x,*) tmp
                    write(y,*) a(j)
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = tmp - a(j)                
            end if
        end do

        ! if(mod(K, 2) == 1) then
        !     tmp = m
        !     do j = 1, N
        !         if(a(j) > 0) then
        !             write(x,*) a(j)
        !             write(y,*) tmp
        !             write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !             tmp = a(j) - tmp
        !         end if
        !     end do
        !     do j = ind+1, N
        !         if(a(j) <= 0) then
        !             write(x,*) tmp
        !             write(y,*) a(j)
        !             write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !             tmp = tmp - a(j)
        !         end if
        !     end do
        ! else
        !     tmp = m
        !     check = .false.
        !     do j = 1, N
        !         if(a(j) > 0) then
        !             if(check .eqv. .false.) then
        !                 write(x,*) tmp
        !                 write(y,*) a(j)
        !                 write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !                 tmp = tmp - a(j)
        !                 check = .true.
        !             else
        !                 write(x,*) a(j)
        !                 write(y,*) tmp
        !                 write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !                 tmp = a(j) - tmp
        !             end if
        !         end if
        !     end do
        !     do j = ind+1, N
        !         if(a(j) <= 0) then
        !             write(x,*) tmp
        !             write(y,*) a(j)
        !             write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
        !             tmp = tmp - a(j)
        !         end if
        !     end do
        ! end if


    end if

    
    deallocate(a)

    stop
contains
end program prob6

Measured memory_bytes: 3948