Takeover Wars
Time limit1sMemory limit128 MB
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 of one corporation absorbs a subsidiary of the other corporation. This is allowed only when the market value of is strictly greater than that of . Subsidiary then disappears from the market, and the market value of does not change (the value gained from '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 and Buyout Limited owns two worth each. Takeover uses its value- subsidiary to absorb one value- subsidiary; even after losing a value- subsidiary to a hostile takeover, it absorbs the remaining value- subsidiary and wins. In the second sample battle Takeover owns and Buyout owns . Takeover has nothing larger than , so it can only keep making friendly takeovers, while Buyout merges its two subsidiaries into one worth ; 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 and (): the number of subsidiaries of Takeover Incorporated and of Buyout Limited.
- The second line contains integers (): the market values of Takeover Incorporated's subsidiaries.
- The third line contains integers (): the market values of Buyout Limited's subsidiaries.
Output
For each test case, print one line in the form Case k: X, where is the test case number starting from and is the corporation that wins the takeover war under optimal play — either Takeover Incorporated or Buyout Limited.