Takeover Wars

Time limit1sMemory limit128 MB

Summary
Two firms alternate merging their own subsidiaries or absorbing a strictly smaller rival one; decide who wins the takeover war with optimal play.
Level

Hard8 of 10

Topics
Greedy, Sorting, Game theory, Implementation
Solved
No attempts yet

Problem

Two large corporations, Takeover Incorporated and Buyout Limited, are waging a takeover war. Each corporation controls a number of subsidiaries, and every subsidiary has a known market value. The goal of the war is to drive the competitor out of the market entirely.

On its turn a corporation performs exactly one takeover, which is either friendly or hostile.

  • Friendly takeover. Two subsidiaries of the same corporation merge into one. The market value of the merged subsidiary is the sum of the two market values, and there is no restriction on the relative sizes of the two subsidiaries.
  • Hostile takeover. A subsidiary AA of one corporation absorbs a subsidiary BB of the other corporation. This is allowed only when the market value of AA is strictly greater than that of BB. Subsidiary BB then disappears from the market, and the market value of AA does not change (the value gained from BB's assets is exactly offset by the cost of the takeover).

You may assume the market values are such that, no matter how the war unfolds, two subsidiaries belonging to different corporations never end up with equal market value.

The corporations move alternately and Takeover Incorporated moves first. On its turn a corporation must perform a takeover if any is available to it; it does nothing only when it cannot make any takeover at all (that is, it has a single subsidiary and that subsidiary cannot absorb any of the opponent's). A corporation loses the war the moment all of its subsidiaries have been taken over.

Determine which corporation is guaranteed to win when both play optimally.

For intuition, consider the first sample battle: Takeover Incorporated owns subsidiaries worth 7,1,17, 1, 1 and Buyout Limited owns two worth 55 each. Takeover uses its value-77 subsidiary to absorb one value-55 subsidiary; even after losing a value-11 subsidiary to a hostile takeover, it absorbs the remaining value-55 subsidiary and wins. In the second sample battle Takeover owns 3,3,3,33, 3, 3, 3 and Buyout owns 5,55, 5. Takeover has nothing larger than 55, so it can only keep making friendly takeovers, while Buyout merges its two subsidiaries into one worth 1010; Buyout wins.

Input

The input contains several test cases and ends at end of file. Each test case is given on three lines.

  • The first line contains two integers NN and MM (1≤N,M≤1051 \le N, M \le 10^5): the number of subsidiaries of Takeover Incorporated and of Buyout Limited.
  • The second line contains NN integers aia_i (1≤ai≤10121 \le a_i \le 10^{12}): the market values of Takeover Incorporated's subsidiaries.
  • The third line contains MM integers bjb_j (1≤bj≤10121 \le b_j \le 10^{12}): the market values of Buyout Limited's subsidiaries.

Output

For each test case, print one line in the form Case k: X, where kk is the test case number starting from 11 and XX is the corporation that wins the takeover war under optimal play — either Takeover Incorporated or Buyout Limited.

Examples1

  1. Example 1

    Input
    3 2
    7 1 1
    5 5
    4 2
    3 3 3 3
    5 5
    
    Expected output
    Case 1: Takeover Incorporated
    Case 2: Buyout Limited