The Coin Game

3.5

34 votes
Easy
Problem

You have three columns of coins where each coin has the same diameter, but they may vary in height. The height of the coin is the same as its value .
Example : 1 Re. coin will have its height as 1 unit. You can change the height of a column by removing and discarding its topmost coin any number of times.
Find the maximum possible height of the columns such that all of the columns are exactly the same height.
This means you must remove zero or more coins from the top of zero or more of the three columns until they're all the same height, then print the height.
The removals must be performed in such a way as to maximize the height.

Note: An empty column is still a column.

Input Format

The first line contains three space-separated integers n1, n2, and n3 describing the respective number of coins in columns 1, 2, and 3. The subsequent lines describe the respective heights of each coin in a column from top to bottom:

  • The second line contains n1 space-separated integers describing the coin heights in column 1.
  • The third line contains n2 space-separated integers describing the coin heights in column 2.
  • The fourth line contains n3 space-separated integers describing the coin heights in column 3.

Constraints

  • 0 < n1,n2,n3 < 105
  • 0 < height of coin < 100

Output Format

Print a single integer denoting the maximum height at which all column will be of equal height.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Initially the heights of the columns 1, 2 and 3 are 8,9 and 7 respectively, which are not same.
If we remove a coin of height 3 from top of column 1, coin of height 4 from top of column 2 and 2 coins of height 1 from top of column 3, the heights of all the three columns become equal with height 5.

  1. 8 - 3 = 5
  2. 9 - 4 = 5
  3. 7 - 1 - 1 = 5

All three columns now have height = 5. Thus, we print 5 as our answer.

Editor Image

?