Friendship and diseases

4

2 votes
Ternary Search, Algorithms
Problem

You want to arrange k people at a table. The table has n rows and m columns. Some of the cells are free and some of them are blocked.

The score of the arrangement is based on two factors:

  • Friendship is always important, even with respect to disease. The ith person has a friendship value fi. A connected component of people with the sum of friendship value equal to s adds s2 to score. A connected component is a set of people adjacent to each other. Two people are adjacent if they share a side.
  • You need to take care of the health of people. The ith person has a danger value di. Two adjacent people i and j reduce the score by di×dj.

Your task is to print an arrangement with as high a score as possible. Note that you can drop a person and do not give him a seat at the table.

Input format

  • The first line contains k,n,m.
  • The second line contains array f.
  • The third line contains array d.
  • The last n lines contain the table.

Output format

Print k lines. In each line, print the row and column of the ith person in the arrangement. Otherwise, print "0 0" if you do not want to include this person in the arrangement.

Constraints

n=m=100

1kn×m

1di,fi1000

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Note that the output is not necessarily the best solution.

The first two persons create a component, the sum of fi equals 3, so it adds 9 to the score.

The third one creates a component with size 1 and sum 3, adds 9 to the score.

The first two persons are adjacent, which decreases the score by 20.

The total score is -2.

Editor Image

?