=solution in GNU C , not fully tested

Right answer but timeout in some cases