Robot Competition
InterviewTime limit1sMemory limit1024 MB
Given the tallest stack height in every row and column of an N by N grid, find the minimum and maximum total number of cubes.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation, Math, Matrix
- Solved
- No attempts yet
Problem
You have built a robot for a robot competition. One of the tasks the robot must solve is to count the number of cubes in a square grid. The grid has rows and columns, and each cell holds a stack of to identical cubes. Unfortunately, your robot is not very good at gathering information. All it can do is find the height of the tallest stack in each row and each column. You planned to compensate for this by making the robot very smart. Write a program that, given the information the robot collected, computes the minimum and maximum possible number of cubes in the grid.
Input
The first line contains the integer , the number of rows and columns in the grid. The second line contains integers , the height of the tallest stack in row . The third line contains integers , the height of the tallest stack in column . The input is guaranteed to contain no contradictions, that is, there is always at least one valid placement of cubes that produces the given values.
Output
Print two integers: the minimum and maximum possible total number of cubes in the grid.
Constraints
Hint

These three are the only possible configurations for the first sample. The first two each have a total of 14 cubes, and the third has a total of 15 cubes. So the answer is that the minimum possible total number of cubes is 14, and the maximum possible is 15.