Cutting Boards

  • + 1 comment

    Here is a python solution . Passes all test cases . Every test case takes under 0.01 secs but #11 , it takes around 6 seconds in python .

    for testcases in range ( int(input())) :
        m , n = [int(x) for x in input().split()]
        cy_ = [int(x) for x in input().split()]
        cx_ = [int(x) for x in input().split()] 
        cy=sorted(cy_,reverse=True)
        cx=sorted(cx_,reverse = True)
        answer = 0 ; hsegm = 1 ; vsegm = 1 ; lenx =n-1 ; leny=m-1
        while True :
            if cy[0] > cx[0] :
                answer += cy[0]*hsegm  
                cy.pop(0)
                leny=leny-1
                vsegm +=1
            else :
                answer += cx[0]*vsegm
                cx.pop(0)
                lenx=lenx-1
                hsegm +=1
            
            if lenx == 0 :
                for cost in cy :
                    answer += cost * hsegm
                break
            elif leny == 0 :
                for cost in cx :
                    answer+=cost*vsegm
                break
        print ( answer % (1000000007) )