408952: GYM103388 J Just Bootfall
Description
Football hasn't always been the most popular sport in the Americas. Historians have found records of an ancient sport that was played in many civilizations across the continent. Because of the lack of spoken tradition about it, the original name is unknown, but in modern times it has been very creatively named "bootfall".
We don't know a lot about bootfall, not even the basic rules. However, archeologists have found a lot of notes made by bootfall coaches when trying to assemble their teams, which give us some information about how teams were formed. These notes are filled with numbers and calculations. Bootfall coaches meticulously tried to optimize their team by assigning players to the best positions possible. To facilitate this task, they developed a metric for determining the performance of each arrangement.
There are $$$M$$$ positions in a bootfall field, which are distributed in a line. A bootfall team consists of $$$N$$$ players, each of which is assigned to some position (all players should be assigned to exactly one position, each position can be occupied by one or more players or can be left unoccupied).
Naturally, players are not equal to each other: Each player can have a different performance when playing in different positions in the field. Concretely, for each player $$$i$$$ and each position $$$j$$$, there is a positive value $$$P_{i,j}$$$ which represents the performance of player $$$i$$$ when playing in position $$$j$$$.
To complicate things further, coaches also consider the aspect of player interaction. Some pairs of players are "best friends". When best friends are far from each other in the field, that harms team performance. There's a positive value $$$C$$$ which represents the performance penalty that is paid when moving best friends away from each other.
Once players are assigned to positions, the value of the team performance is calculated as follows: First, we add up the performances of the players when playing in their assigned position. Then, for each pair of players who are best friends, we subtract $$$C$$$ times the distance between the two players, where the distance between two players is defined as the difference (in absolute value) between the positions to which the players were assigned.
We want to know how good bootfall coaches were at forming teams. In order to do that, we would like to know what is the maximum possible value of team performance achievable by arranging the players in the optimal positions, given the performances of the players in each position and the pairs of players who are best friends.
InputThe first line contains four integers $$$N$$$, $$$M$$$, $$$K$$$ and $$$C$$$ ($$$1 \le N,M \le 50$$$, $$$0 \le K \le 50$$$, $$$0 \le C \le 10^6$$$), representing the number of players, the number of positions, the number of pairs of close friends and the penalty for having close friends far from each other.
Each of the next $$$N$$$ lines contains $$$M$$$ integers. The $$$j$$$-th integer of the $$$i$$$-th line is $$$P_{i,j}$$$, representing the performance of player $$$i$$$ if playing in position $$$j$$$ ($$$0 \le P_{i,j} \le 10^6$$$).
Each of the next $$$K$$$ lines contains 2 integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i < b_i \le N$$$), which represent that players $$$a_i$$$ and $$$b_i$$$ are close friends. No two pairs of players are repeated in this list.
OutputOutput a line containing one integer, representing the maximum team performance possible.
ExampleInput3 3 2 5 5 2 1 3 2 8 1 9 3 1 2 1 3Output
14