## WC '17 Contest 4 S3 - Guardians of the Cash

View as PDF

Points: 15 (partial)
Time limit: 1.8s
Memory limit: 64M

Author:
Problem type
##### Woburn Challenge 2017-18 Round 4 - Senior Division

With the fate of the entire galaxy on the line, the Guardians of the Galaxy have naturally been called in to battle against Thanos as well! That being said, their leader, Star-Lord, has realized the potential for a bit of financial compensation along the way. He's successfully secured a hefty stash of solid gold coins for his team before any fighting has even begun!

While waiting for the action to finally get underway, Rocket Raccoon and Groot have decided to amuse themselves with a game involving the coins. For starters, they've drawn a grid on the ground with rows and columns . Then, in each cell (, ) (the cell in the -th row and -th column), they've placed a stack of coins.

If you look at the resulting coin collection from the South (from beyond row ), all you can make out is the shape of its "skyline". This Southern skyline is a sequence of positive integers, the -th of which is the number of coins in the tallest stack in the -th column. Similarly, its Western skyline (when viewed from beyond column ) is a sequence of positive integers, the -th of which is the number of coins in the tallest stack in the -th row.

For example, consider the following arrangement of stack heights:

2 3 2
4 5 4
5 1 2

Its Southern skyline sequence is , while its Western skyline sequence is .

Rocket Raccoon now poses the following challenge to Groot: Remove or more coins from the collection such that it will look exactly the same from the South as from the West (in other words, such that its Southern skyline sequence will be equal to its Western skyline sequence). Groot has a bit of trouble lifting the heavy gold coins, so he'd like to pass Rocket Raccoon's challenge while removing as few of them as possible. What's the minimum number of coins which he must remove from the stacks in total? Note that this value may not fit into a -bit signed integer.

In test cases worth of the points, and for each cell (, ).
In test cases worth another of the points, .

#### Input Specification

The first line of input consists of a single integer, .
lines follow, the -th of which consists of integers, , for .

#### Output Specification

Output a single integer, the minimum number of coins which Groot must remove.

#### Sample Input

3
2 3 2
4 5 4
5 1 2

#### Sample Output

4

#### Sample Explanation

One option is for Groot to remove one coin from the stack in cell (, ), one coin from (, ), and two coins from (, ), resulting in the following arrangement of stack heights:

2 3 2
3 5 3
3 1 2

Note that both its Southern and Western skyline sequences are equal to .